2012-10-12 72 views
0

我有一些拆分字符串的問題。拆分字符串接受一些字符

我的字符串:

"SG_ PJB_ : 1|[email protected]+ (0.25,-60) [-60|195.75] "Degrees Celcius" PCM,TCM,AFCM"; 

我想有:

SG_ 
PJB_ 
1 
10 
0+ 
0.25 
-60 
-60 
195.75 
Degrees Celcius 
PCM 
TCM 
AFCM 

我的代碼,我曾嘗試:

string s = "SG_ PJB_ : 1|[email protected]+ (0.25,-60) [-60|195.75] \"Degrees Celcius\" PCM,TCM,AFCM"; 
string[] res = s.Split(new char[] { ' ', ',', ':', '|', '@', '(', ')', '[', ']', ';' }, StringSplitOptions.RemoveEmptyEntries); 
for (int i = 0; i < res.Length; i++) 
{ 
    // Console.WriteLine("   "); 
    Console.WriteLine("{0}", res[i]); 
} 
Console.ReadKey(); 

但在2個陣列splited 「攝氏度」 。我該怎麼辦?

+0

'度Celcius'有一個空間在那裏。當然它分裂了。你不能在一個'string.Split'中實現你所要求的。 – Oded

+0

我應該做2分裂嗎?將firstordeufault劃分爲「lastordefaut from」? –

+0

你可以在'''上分割,然後在第一個'''之前分割東西,然後在其他項目之後分割東西,然而,這個假設你只會在字符串中有一對'「' 。 – Oded

回答

1

用這樣的方式:

var result = input.Split(new[] { '"' }).SelectMany((s, i) => 
    { 
     if (i%2 == 1) return new[] {s}; 
     return s.Split(new[] { ' ', ',', ':', '|', '@', '(', ')', '[', ']', ';' }, 
           StringSplitOptions.RemoveEmptyEntries); 
    }).ToList(); 
+0

非常感謝anh Cuong :) –

+0

1問題更多,我怎麼知道每個數組的索引使用它? –

+0

@UniLe:不明白,你可以舉例 –