Click here to Skip to main content
12,406,592 members (49,485 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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 3-Sep-12 1:40am
Updated 3-Sep-12 1:41am
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

try this:
string part=Mystring.SubString(0,Mystring.IndexOf(','));
  Permalink  
v2
Comments
Kuthuparakkal 3-Sep-12 8:19am
   
5+
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

This should work:

string s = "abcd, cdef, efg, ijk, lmn";
 
string result = s.Substring(0, s.IndexOf(","));
  Permalink  
Comments
Prasad_Kulkarni 3-Sep-12 8:22am
   
Good one +5
Rahul Rajat Singh 3-Sep-12 8:23am
   
Thanks.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

This should work:

string input = "abcd , cdef , efg , ijk , lmn";
 
string[] splitString = input.split(',');
 
string result = splitString[0].Trim();
  Permalink  
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!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Hello

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

string ReplaceText = @" abcd , cdef , efg , ijk , lmn";
ReplaceText = ReplaceText.Substring(0, ReplaceText.IndexOf(","));
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160730.1 | Last Updated 3 Sep 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100