Separate the line after the decimal point to the end of the line - asp.net C #

I have a string like

ishan, training

I want to split the line after "," ie I want the result to be

Training

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

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

Of all the lines above, I need the part after the ","

+8
source share
7 answers

You can use String.Split :

 string[] tokens = str.Split(','); string last = tokens[tokens.Length - 1] 

Or, a little easier:

 string last = str.Substring(str.LastIndexOf(',') + 1); 
+17
source share
 var arr = string.Split(","); var result = arr[arr.length-1]; 
+2
source share

sourcestring.Substring(sourcestring.IndexOf(',')) . You can check sourcestring.IndexOf(',') for -1 for strings without sourcestring.IndexOf(',')

+2
source share

I know that this question has already been answered, but you can use linq:

 string str = "1,2,3,4,5"; str.Split(',').LastOrDefault(); 
+2
source share

Use String.Split(",") assign the result to an array of strings and use what you want.

0
source share

Here is the VB version. I'm sure it’s easy to translate it to C #, although

  Dim str as string = "ishan,training" str = str.split(",")(1) return str 
0
source share

Although there are a few notes that mention the issue with multiple commas, there is no mention of a solution for this:

 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"

0
source share

All Articles