如何分割字符串 「test1&test2
」 到 「test1
」 「test2
」 無定界符 「&
」 在C#分割字符串C#
function("test1&test2","&") == "test1"
function("test1&test2","&") == "test2"
如何分割字符串 「test1&test2
」 到 「test1
」 「test2
」 無定界符 「&
」 在C#分割字符串C#
function("test1&test2","&") == "test1"
function("test1&test2","&") == "test2"
string[] parts = "test1&test2".Split(new char[]{'&'});
這麼簡單。
String.Split
返回string[]
,第一元件是 「測試1」,第二個是 「test2的」:
string[] split = "test1&test2".Split('&');
string test1 = split[0];
string test2 = split[1];
String.Split
方法是你在找什麼:
string input = "test1&test2";
string[] parts = input.Split(new[] { '&' });
我相信你的意思是'&'作爲你的分隔符。這會給你{「test1&test2」} – 2013-04-09 21:22:55
不需要建立一個字符數組,string.Split需要一個參數,所以傳遞一個簡單的字符'&'也適用 – Steve 2013-04-09 21:28:14
這是一個簡單string.Split
string example = "test1&test2";
string[] parts = example.Split('&');
Console.WriteLine(parts[0]);
Console.WriteLine(parts[1]);
注意,以downvoter是String.Split需要params char[]
,所以也傳遞一個簡單的炭工程....
你熟悉與'string.Split()'方法? – MethodMan 2013-04-09 21:20:12
https://www.google.com/search?q=c%23+split+string+with+delimiter – spender 2013-04-09 21:22:09
此問題未顯示研究工作。在Google中輸入「Split String C#」,結果非常豐富,其中包括豐富的有用方法文檔。我們期望的#1事情是讓你在提出問題之前[做你自己的研究](http://stackoverflow.com/questions/how-to-ask)。 – 2013-04-09 21:22:55