string manipilation question

  • Thread starter Thread starter Yoavo
  • Start date Start date
Y

Yoavo

Hi,
I have a string which contains the character ','
I want to remove all the text after this character.
For example:
if my string was
string mystr = "aa,bb,cc,dd";

I want that after the operation I will get:
mystr = "aa"

How do I do it ?
Yoav.
 
Hi Yoavo,
one way would be:

string abc = "aa,bb,cc,dd";
int commaIndex = abc.IndexOf(',');
if (commaIndex != -1)
{
string after = abc.Substring(0, abc.IndexOf(','));
}

Hope that helps
Mark.
http://www.markdawson.org
 
Yoavo said:
Hi,
I have a string which contains the character ','
I want to remove all the text after this character.
For example:
if my string was
string mystr = "aa,bb,cc,dd";

I want that after the operation I will get:
mystr = "aa"

How do I do it ?
Yoav.

Another possibility, depending upon your needs, is:

string[] parts = mystr.Split(',');
mystr = parts[0];

This is handy if you need not just the first bit ("aa") but some of the
others as well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top