Skip to content
Loading
  • Ganesan C
    +1
    Hi, Try below code.
     
    1. Console.WriteLine("Author Name");    
    2. foreach (var author in AuthorsList)    
    3. {    
    4. var name =  author.Split(":")[1];    
    5. Console.WriteLine(name);    
    6. }    
    7.     
    8.   
    9. Console.WriteLine("Author City");    
    10. foreach (var author in AuthorsList)    
    11. {    
    12. var city =  author.Split(":")[2];    
    13. Console.WriteLine(city);    
    14. }    
     
    +1
  • +1

    public class Program
    {
        public static void Main()
        {
             List AuthorList = new List();

                // Add items using Add method   
                AuthorList.Add("Name :Mahesh Chand,Adress:Hyd");
                AuthorList.Add("Name:Praveen Kumar,Adress:Pune");
                AuthorList.Add("Name:Nipun Tomar,Adress:Delhi");
              


                //AuthorName  
                Console.WriteLine("Output Author Name and Adress");
                foreach (string author in AuthorList)
                {
                    var authorName = author.Split(Convert.ToChar(":"))[1];
                    var authorCity = author.Split(Convert.ToChar(":"))[2];
                    Console.WriteLine(authorName);
                     Console.WriteLine(authorCity);
                }

            
        }
    }

    Ouput;

    Output Author Name and Adress
    Mahesh Chand,Adress
    Hyd
    Praveen Kumar,Adress
    Pune
    Nipun Tomar,Adress
    Delhi

    Still not solved my quetion

    +1
  • Anu Viswan
    +1
    Since most of the earlier answers are based on string.split, here is another approach using the Regular expressions.
     
     
    1. var nameList = new List<string>();      
    2. var addressList = new List<string>();      
    3.       
    4. var regex = new Regex(@"^Name\s*:(?[^,]*\s*),\s*Adress\s*:(?\w*\s*)$");      
    5. foreach(var author in AuthorList)      
    6. {      
    7.     var match = regex.Match(author);      
    8.     if(match.Success)      
    9.     {      
    10.         nameList.Add(match.Groups["NameValue"].Value);      
    11.         addressList.Add(match.Groups["AddressValue"].Value);      
    12.     }      
    13. }     
    14.   
    15. foreach(var author in nameList) Console.WriteLine(author);  
    16.   
    17. foreach(var address in addressList) Console.WriteLine(address); 
    +1
  • Satya Karki
    +1
    Try this
     
    1. List<string> AuthorList = new List<string>();    
    2.     
    3. // Add items using Add method     
    4. AuthorList.Add("Name :Mahesh Chand,Adress:Hyd");    
    5. AuthorList.Add("Name:Praveen Kumar,Adress:Pune");    
    6. AuthorList.Add(" ");    
    7. AuthorList.Add("Name:Nipun Tomar,Adress:Delhi");    
    8. AuthorList.Add("Name:Dinesh Beniwal");    
    9. //AuthorName    
    10. Console.WriteLine("Output Author Name");    
    11. foreach (string author in AuthorsList)    
    12. {    
    13. var authorName =  author.Split(":")[1];    
    14. Console.WriteLine(authorName);    
    15. }    
    16.     
    17.     
    18. //AuthorCity    
    19. Console.WriteLine("Output Author City");    
    20. foreach (string author in AuthorsList)    
    21. {    
    22. var authorCity =  author.Split(":")[2];    
    23. Console.WriteLine(authorCity);    
    24. }   
    +1