Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

split string after comma and till string ends- asp.net c#

Tags:

c#

asp.net

I have a string of type

ishan,training

I want to split the string after "," i.e i want the output as

training

NOTE: "," does not have a fixed index as the string value before "," is different at different times.

e.g ishant,marcela OR ishu,ponda OR amnarayan,mapusa etc...

From all the above strings i just need the part after ","

like image 426
Ishan Avatar asked Apr 13 '11 10:04

Ishan


People also ask

How do you break a string with a comma in it?

In C#, a string can be broken by one or more given delimiters by using the Split method. The simple way of using the Split method can be: Where Source_string is the string that you want to break. The delimiter like a comma, space etc. is specified after the Split in parenthesis.

How to split strings in C #?

Split String In C#. Learn different ways to Split Strings in C#. A string can be splitted in C# separated by a character delimiter, string delimiter, and an array of characters.

How to split a common phrase into an array of strings?

The following code splits a common phrase into an array of strings for each word. C#. string phrase = "The quick brown fox jumps over the lazy dog."; string[] words = phrase.Split (' '); foreach (var word in words) { System.Console.WriteLine ($"<{word}>"); }. Every instance of a separator character produces a value in the returned array.

What is the use of the split () method in JavaScript?

The Split () method returns an array of strings generated by splitting of original string separated by the delimiters passed as a parameter in Split () method. The delimiters can be a character or an array of characters or an array of strings.


1 Answers

Although there are several comments mentioning the issue of multiple commas being found, there doesn't seem to be any mention of the solution for that:

string input = "1,2,3,4,5";
if (input.IndexOf(',') > 0)
{
    string afterFirstComma = input.Split(new char[] { ',' }, 2)[1];
}

This will make afterFirstComma equal to "2,3,4,5"

like image 186
Jaymz Avatar answered Oct 12 '22 20:10

Jaymz