Alan Alan - 2 months ago 7
C# Question

How can I split a string like "12-15" into two numbers?

My front-end application sends strings that look like this:

"12-15"


to a back-end C# application.

Can someone give me some pointers as to how I could extract the two numbers into two variables. Note the format is always the same with two numbers and a hyphen between them.

Answer

Split the string into parts:

string s = "12-15";
string[] num = s.Split('-');
int part1 = Convert.ToInt32(num[0]);
int part2 = Convert.ToInt32(num[1]);