Click here to Skip to main content
15,886,095 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello ,

I have a label whose text is like
 abcd , cdef , efg , ijk , lmn <pre>

i need to get the part before the first occurrence of ' , ' ie abcd. 

 i thought of using string.split() or string.substring() but it's not generic as i need to have the position of the characters which i don't have. i need something generic.
Posted
Updated 3-Sep-12 1:41am
v2

try this:
C#
string part=Mystring.SubString(0,Mystring.IndexOf(','));
 
Share this answer
 
v2
Comments
Kuthuparakkal 3-Sep-12 8:19am    
5+
This should work:

C#
string s = "abcd, cdef, efg, ijk, lmn";

string result = s.Substring(0, s.IndexOf(","));
 
Share this answer
 
Comments
Prasad_Kulkarni 3-Sep-12 8:22am    
Good one +5
Rahul Rajat Singh 3-Sep-12 8:23am    
Thanks.
This should work:

C#
string input = "abcd , cdef , efg , ijk , lmn";

string[] splitString = input.split(',');

string result = splitString[0].Trim();
 
Share this answer
 
Comments
__TR__ 3-Sep-12 7:48am    
5+
Manas Bhardwaj 3-Sep-12 7:49am    
thx!
Prasad_Kulkarni 3-Sep-12 8:21am    
My 5!
Manas Bhardwaj 3-Sep-12 8:41am    
thx!
srasteh 15-Aug-18 15:59pm    
Thanks
Hello

Please check below code it will use for getting string as per your need

C#
string ReplaceText = @" abcd , cdef , efg , ijk , lmn";
ReplaceText = ReplaceText.Substring(0, ReplaceText.IndexOf(","));
 
Share this answer
 
Comments
Kuthuparakkal 3-Sep-12 8:19am    
inefficient

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