2016-12-28 25 views
-4

什麼是例如好辦法,如果我有字符串:符號作爲單獨的值之後,如何得到字符串的一部分,直到一定的符號,然後部分

string str = "hello, world = how are you~hello"; 

和我一組允許的正文是特定的符號: 「=」,「~」,「\」,「.」,「^」,「#

,如果字符串中包含任何這樣的符號,第一個GET值,或正確地說,整個的存在字符串的一部分在特定的符號之前,然後在特定之後分別得到部分字符串無花果標誌。

hello, world 
how are you 
hello 

向該字符串中附加的每個單獨的變量爲第一3個符號:

string part1 = hello, world 
string part2 = how are you 
string part3 = hello 

所以,如果字符串是:

string str = "hello, world = how are you~hello \ ok"; 

結果將是:

string part1 = hello, world 
string part2 = how are you 
string part3 = hello \ ok 
+1

寫一個正則表達式,可以做你想做的事。如果遇到麻煩,請向您遇到的具體問題尋求幫助。這不是一個免費的代碼寫入服務。 –

+0

提供一些代碼然後問問題 – Arash

回答

2

有一個overload of string.Split,使這個非常容易:

string str = "hello, world = how are you~hello \\ ok"; 
var results = str.Split(new [] { '=', '~', '\\', '.', '^', '#'}, 3); 

// results[0] = "hello, world " 
// results[1] = " how are you" 
// results[2] = "hello \ ok" 

第二個參數是要獲取子串的最大數量。

相關問題