Another Salesforce blog!!!

Salesforce, Apex

Extract substring from a string until finds a comma c# asp.net

substring = str.Split(‘,’)[0];

or

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);

or

string NoComma = "";
string example = "text before first comma, more stuff and another comma, there";
string result = example.IndexOf(',') == 0 ? NoComma  : example.Split(',')[0];

or

myString = myString.Substring(0,myString.IndexOf(','));

Leave a Reply

Your email address will not be published. Required fields are marked *