C# 如果遇到任何字符,则分隔两个字符串

C# 如果遇到任何字符,则分隔两个字符串,c#,string,C#,String,如何将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[] C

如何将www.myurl.com/help、mycustomers分为www.myurl.com/helpmycustomers,并将它们放在不同的字符串变量中?

尝试以下方法:

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 s = "www.myurl.com/help,mycustomers";
string first = s.Split(new []{',', '#'},
                       StringSplitOptions.RemoveEmptyEntries)[0];
string second = s.Split(new [] { ',', '#' },
                        StringSplitOptions.RemoveEmptyEntries)[1];
作为Steve,使用indexer可能不好,因为您的字符串不能有任何
#

您也可以使用
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 s=“www.myurl.com/help,mycustomers”;
var array=s.Split(新[]{',','#'},
StringSplitOptions.RemoveEmptyEntries);
for(int i=0;i
您可以有一个简短而甜蜜的解决方案:

string[] myArray= "www.myurl.com/help,mycustomers".Split(',');

你说的“任何角色”是什么意思?听起来你只是想用逗号分开。你看过
string.Split
了吗?它可以是逗号或散列…下次再清楚一点。现在我们有4个答案需要更新为“哈希”位。。。。顺便问一下,什么是散列?我的应用程序读取某种qrcode并获取一个字符串…它应该提取由散列或逗号分隔的子字符串@SteveI希望向上投票,但在拆分后自动使用索引器并不是一个好方法。你知道,如果没有。。。。。。