Reputation: 173
I have string 'name' and want to do substring the last number from this string.
string name = "1100_PF_R_06230_1";
textBox1.Text = (name.Substring(name.Length - 1, 1));
name string changes respectively as file number, so 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 to 10 the my substring gives me 0 and file starts from 1 again. i want to substing name from last '_' underscore so that i can add number to it.
please help.
Upvotes: 4
Views: 193
Reputation: 19828
Try this code:
string name = "1100_PF_R_06230_1";
var num = (name.Substring(name.LastIndexOf('_')+1));
Upvotes: 4
Reputation: 23831
You want to use Split('_')
as follows
string strNumber = name.Split('_').Last();
I hope this helps.
Upvotes: 3