Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Substring in c# and add one by one

Tags:

c#

.net

wpf

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.

like image 384
user1065542 Avatar asked Dec 07 '22 08:12

user1065542


1 Answers

You can use Split method with LINQ Last:

var result =  name.Split('_').Last();
like image 161
cuongle Avatar answered Dec 11 '22 07:12

cuongle