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 ","
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.
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.
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.
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.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With