Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
I have a string variable like string A = "Name:venkat,PhNo:77089,City:Test" i want to get venkat and 77089 and Test values then store it separate variable using c#

I want simple code how to get it.

Thanks & Regards,
venkat
Posted
Updated 22-Jan-15 1:32am
v2
Comments
CHill60 22-Jan-15 7:32am    
What have you tried?
venkat167 22-Jan-15 7:47am    
i have string A = "Name:venkat,PhNo:77089,City:Test" .I want get value venkat,77089 from "string A" without Name and PhNo.Can u send any example program c#
André Kraak 22-Jan-15 7:35am    
Take a look at the String.Split[^] method.

You can use String.Split with ','. You will get 3 strings in key:value format.
Then you can easily take values out from it.
Refer this[^] for using string.split
 
Share this answer
 
Comments
venkat167 22-Jan-15 7:43am    
could you give any sample program for that.
nagendrathecoder 22-Jan-15 7:45am    
The link i have provided has some sample programs, you can refer those.
You can also do the same using Regular Expression.

C#
String tmp = @":(\w+)";
String input = "Name:venkat,PhNo:77089,City:Test";

Regex exp = new Regex(tmp);
MatchCollection col = exp.Matches(input);


You can further eliminate ":" in the array
 
Share this answer
 
Further to the other solutions...

You can split on multiple characters. For example
C#
string A = "Name:venkat,PhNo:77089,City:Test";

//Attempt 1 - split on both : and , to get all of the parts separately
var attempt1 = A.Split(':',',');
foreach (var s in attempt1)
    Console.WriteLine(s);
Console.WriteLine("Stuff I need : {0}, {1}, {2}", attempt1[1], attempt1[3], attempt1[5]);
gives results of
Name
venkat
PhNo
77089
City
Test
Stuff I need : venkat, 77089, Test

Which is fine if the data Is always in that order. But what if this string is a string of named parameters to a console application for example. Imagine the user has not quite got it right and sends in a string that looks like
A = "Name:venkat,City:Test,Another:Thing,PhNo:77089";
Then you would get
Stuff I need : venkat, Test, Thing
Hm, not what I was wanting.

However, if you split just on the comma (,) you will essentially get a list of paired data itemname:value
C#
A = "Name:venkat,City:Test,Another:Thing,PhNo:77089";
var attempt2 = A.Split(',');
foreach (var s in attempt2)
    Console.WriteLine(s);
gives results
Name:venkat
Another:Thing
City:Test
PhNo:77089
which you can then search for the items you need rather than using hard-code positions
C#
//Find just the PhNo
var res = attempt2.ToList()
                    .Find(x => x.StartsWith("PhNo"))
                    .Split(':')
                    [1];
Console.WriteLine("The Phone number: {0}", res);
gives
The Phone number: 77089

Obviously a lot more long-winded than the first attempt, and longer-winded than the regex solution, but I suggest more flexible, particularly if you have more items to add to the list.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900