Click here to Skip to main content
15,892,517 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
i have a string formatted like shakun(shrestha)

I need to retrieve sub string as "shakun" and "shrestha" seperately

i tried to using the split function but my output was shrestha)

here the code snippet i tried.
C#
string test = "shakun(shrestha)";
string s = test.Split('(')[1];

Any help would be appreciated :)
Posted
Updated 31-Jul-11 21:20pm
v4
Comments
walterhevedeich 1-Aug-11 2:45am    
Im quite confused by your code. is shakun a method?
Sergey Alexandrovich Kryukov 1-Aug-11 2:48am    
Apparently not. This is a reason of my vote of 1. :-)
--SA
walterhevedeich 1-Aug-11 3:28am    
He probably typed it instead of doing a copy paste. Good for him his problem was already solved.
sanomama 1-Aug-11 3:18am    
shakun is just a string.
Amund Gjersøe 1-Aug-11 3:21am    
I updated the code accordingly.

C#
string test = "shakun(shrestha)";
test = test.Replace("(", "|");
test = test.Replace(")", "|");
string[] arr = test.Split('|');
foreach (string s in arr)
{
    //do what you want here.

}
 
Share this answer
 
Hi,

Try this


C#
string test =shakun(shrestha);
            string s = test.Split('(',')')[1];
 
Share this answer
 
v3
Comments
sanomama 1-Aug-11 3:20am    
thanks worked as a charm.
Dalek Dave 1-Aug-11 3:26am    
Good Call, get a 5!
Syed Salman Raza Zaidi 1-Aug-11 3:31am    
Thanks :)
Hope it helps,

C#
string test = "shakun(shrestha)";
           
string[] s = test.Split(new char[] { '(', ')' }, StringSplitOptions.None).Where(item => item != string.Empty).ToArray<string>();

:)
 
Share this answer
 
C#
char[] array = { '(', ')' };

            string[] splitArray = str.Split(array);
            string strFirst = splitArray[0];
            string strSirst = splitArray[1];
 
Share this answer
 
Comments
sanomama 1-Aug-11 3:21am    
thanks.
Dalek Dave 1-Aug-11 3:27am    
Nice Answer.
DipaliKolhe 1-Aug-11 19:20pm    
Thanks Dalek Dave!
If you mean string test = "shakun(shrestha)";, then your output is correct.
That is what splitting on ( will return.
 
Share this answer
 
Comments
sanomama 1-Aug-11 3:19am    
but i need the output as shrestha only not as shrestha)
Abhinav S 1-Aug-11 4:35am    
Well remove the last character from the string.

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