如何将 www.myurl.com/help,mycustomers 分为 www.myurl.com/help 和mycustomers并将它们放入不同的字符串变量中?
试试这个:
string MyString="www.myurl.com/help,mycustomers";
string first=MyString.Split(',')[0];
string second=MyString.Split(',')[1];
如果MyString包含多个部分,您可以使用:
string[] CS = MyString.Split(',');
每个部分都可以像以下方式访问:
CS[0],CS[1],CS[2]
例如:
string MyString="www.myurl.com/help,mycustomers,mysuppliers";
string[] CS = MyString.Split(',');
CS[0];//www.myurl.com/help
CS[1];//mycustomers
CS[2];//mysuppliers
如果您想了解有关拆分功能的更多信息。阅读此内容。
它可以是逗号或哈希
然后你可以使用String.Split(Char[])
方法,如;
string s = "www.myurl.com/help,mycustomers";
string first = s.Split(new []{',', '#'},
StringSplitOptions.RemoveEmptyEntries)[0];
string second = s.Split(new [] { ',', '#' },
StringSplitOptions.RemoveEmptyEntries)[1];
正如史蒂夫指出的那样,使用索引器可能不好,因为您的字符串不能有任何,
或#
。
你也可以使用循环for
喜欢;
string s = "www.myurl.com/help,mycustomers";
var array = s.Split(new []{',', '#'},
StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine(string.Format("{0}: {1}", i, array[i]));
}
你可以有一个简短而甜蜜的解决方案,如:
string[] myArray= "www.myurl.com/help,mycustomers".Split(',');