简体   繁体   中英

Separating Comma-Separated Strings

I have a string containing the value "12,13" I want 2 strings from this string ie s1 = 12 and s2 = 13

how can I separate in asp.net C#?

I have not much time to google it that's why I am posting it here. Hope anyone can answer this.

Thanks in advance

Try this:

string firstString = "12,13";
string[] values = firstString.Split(',');

string s1 = values[0];
string s2 = values[1];
string numbersText ="12,13"
string[] numbers= numbersText.Split(','); 
if(numbers.Length >= 2) // check length of array if you not sure about text before access items of it 
{
    string s1 =numbers[0];
    string s1 =numbers[1];
}

You can use string.Split() method

example:

string text = "12,13";
string[] parts = text.Split(',');

string s1 = parts[0]; // "12"
string s2 = parts[1]; // "13"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM