简体   繁体   English

分隔逗号分隔的字符串

[英]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 我有一个包含值"12,13"字符串,我希望从该字符串中获得2个字符串,即s1 = 12s2 = 13

how can I separate in asp.net C#? 如何在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 您可以使用string.Split()方法

example: 例:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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