Substring in C # and adding one at a time

I have a string 'name' and want to make the substring the last number from this string.

string name = "1100_PF_R_06230_1"; textBox1.Text = (name.Substring(name.Length - 1, 1)); 

the name string changes accordingly as the file number, so the string name becomes.

 1100_PF_R_06230_1 1100_PF_R_06230_2 1100_PF_R_06230_3 1100_PF_R_06230_4 1100_PF_R_06230_5 1100_PF_R_06230_6 1100_PF_R_06230_7 1100_PF_R_06230_8 1100_PF_R_06230_9 1100_PF_R_06230_10 

when it reaches 10, my substring gives me 0, and the file starts again with 1. I want to substitute the name from the last underscore '_' so that I can add a number to it.

please, help.

+4
source share
3 answers

Try this code:

  string name = "1100_PF_R_06230_1"; var num = (name.Substring(name.LastIndexOf('_')+1)); 
+4
source

You can use the Split method with LINQ Last :

 var result = name.Split('_').Last(); 
+8
source

You want to use Split('_') as follows

 string strNumber = name.Split('_').Last(); 

Hope this helps.

+3
source

All Articles