Alan Alan - 1 year ago 61
C# Question

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

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


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 Source

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]);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download