Alex Rivera | Logout

Extract substring from a string until finds a comma

Asked 2009-09-26T08:52:40.897
21

I'm building a page and would like to know how to extract substring from a string until finds a comma in ASP.Net C#. Can someone help please?

Edit
Report

2 Answers

10

You can use IndexOf() to find out where is the comma, and then extract the substring. If you are sure it will always have the comma you can skip the check.

        string a = "asdkjafjksdlfm,dsklfmdkslfmdkslmfksd";
        int comma = a.IndexOf(',');
        string b = a;
        if (comma != -1)
        {
            b = a.Substring(0, comma);
        }
        Console.WriteLine(b);
answered 2009-09-26T08:59:33.967
9
myString = myString.Substring(0,myString.IndexOf(','));
answered 2009-09-26T08:56:12.997

Your Answer