2011-09-08 89 views
2

我有這個字符串使用C#分割字符串分隔符多

"abc,\u000Bdefgh,\u000Bjh,\u000Bkl"

,我需要拆分C#中的字符串,每次,\u000B出現應該是一個新詞。

我嘗試這樣做:

string[] newString = myString.Split(",\u000B"); 

,但它沒有工作,我怎麼能做到這一點?

+1

這只是一個長度爲兩個字符的分隔符。它怎麼不起作用? – BoltClock

+0

@BoltClock - 可能因爲Split沒有超載,只需要一個字符串作爲參數。 –

+2

我知道,我促使OP說的不僅僅是「它沒有工作」。 – BoltClock

回答

10

更改split命令是:

string[] newString = ip.Split(new[]{",\u000B"}, StringSplitOptions.RemoveEmptyEntries); 

或使用,StringSplitOptions.None如果您想保留空項,而分裂。

2
 string myString = "abc,\u000Bdefgh,\u000Bjh,\u000Bkl"; 

     string[] a = myString.Split(new string[] { ",\u000B" }, StringSplitOptions.RemoveEmptyEntries); 
+0

謝謝,但我接受了我的第一個答案。 – Ovi

+2

:)實際上我在1分鐘內跑得更快,但沒關係。你接受的答案看起來更多解釋 – Samich

3
string[] newString = myString.Split(new string[] { ",\u000B" }, StringSplitOptions.None); 

作品在我的機器

+0

謝謝,但我接受了我得到的第一個答案 – Ovi

0

上你可以使用短字符轉義符號: 「\ V」代替。

Short UTF-16 Description 
-------------------------------------------------------------------- 
\'  \u0027 allow to enter a ' in a character literal, e.g. '\'' 
\"  \u0022 allow to enter a " in a string literal, e.g. "this is the double quote (\") character" 
\\  \u005c allow to enter a \ character in a character or string literal, e.g. '\\' or "this is the backslash (\\) character" 
\0  \u0000 allow to enter the character with code 0 
\a  \u0007 alarm (usually the HW beep) 
\b  \u0008 back-space 
\f  \u000c form-feed (next page) 
\n  \u000a line-feed (next line) 
\r  \u000d carriage-return (move to the beginning of the line) 
\t  \u0009 (horizontal-) tab 
\v  \u000b vertical-tab