Click here to Skip to main content
14,828,868 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey All,
I have a textbox.
In that some entries are made like
textbox.text = Tendulkar,Sachin(srt);Dhoni,MS(msd);Sehwag,Virender(viru);D'Souza,Tim(tdsc)

I want an array of string which will have values like (srt,msd,viru,tdsc).
I guess we will first have to go around splitting by (';') and get a result array set of this.
and then split with ('(') and (')').
A for loop inside another is what i guess.

How can i get this??
Appreciate if u cud help me out.

[no name] 20-Oct-13 8:46am
You described what you have to do. Just do it....(N.B it is not a "for" inside a "for", like you described it: First you separate by ";" and afterwards you analyze the separated strings for "(...)").

But try to also to think forward. E.g. What should happen if somebody enters "Tendulkar(xyz),Sachin(srt);Dhoni,MS(msd);Sehwag,Virender(viru);D'Souza,Tim(tdsc)"

...first have a look to OriginalGriff's answer ;)

1 solution

Easier way is to use a Regex:
string s = "Tendulkar,Sachin(srt);Dhoni,MS(msd);Sehwag,Virender(viru);D'Souza,Tim(tdsc)";
Regex reg = new Regex(@"(?<=\().+?(?=\))");
MatchCollection matches = reg.Matches(s);
List<string> separated = new List<string>();
foreach (Match m in matches)
aks.shan 20-Oct-13 8:55am
Hi Original Griff,
This works wonders.
Would appreciate if u could help me in understanding regex.
OriginalGriff 20-Oct-13 9:26am
There are whole books on Regular expressions!
This one is good:
As is this:
But look at Google and you'll find loads of basic tutorials which will explain them better than I can in a small textbox...
This is worth downloading as well:
Expresso - it's free, and it examines and generates Regular expressions.
aks.shan 20-Oct-13 10:17am
Thanks :)
OriginalGriff 20-Oct-13 10:28am
You're welcome!

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