2013-04-18 127 views
-3

在我的c#程序中,我有一個像這樣的字符串。如何從c中的字符串中分離字符串#

String ss = [["Karoline,Ejlstrupvej 90 90, 4100, Ringsted,07:50:00,55.48148, 11.78890","Karoline,Ejlstrupvej 101, 4100, Ringsted,07:50:00,55.47705, 11.78523","Byskovskolen, Prstevej 19, 4100, Ringsted,55.46842, 11.80975"],["Mads,Sdr. Parkvej 27, 4100, Ringsted,08:00:00,55.44648, 11.78757","Niels,Fluebækvej 204, 4100, Ringsted,08:00:00,55.44295, 11.79137","Heldagsskolen Specialtilbud, Vestervej 27, 4100, Ringsted,55.44050, 11.78115"]]; 

我該如何區分這樣的值。

["Karoline,Ejlstrupvej 90 90, 4100, Ringsted,07:50:00,55.48148, 11.78890","Karoline,Ejlstrupvej 101, 4100, Ringsted,07:50:00,55.47705, 11.78523","Byskovskolen, Prstevej 19, 4100, Ringsted,55.46842, 11.80975"] 

    ["Mads,Sdr. Parkvej 27, 4100, Ringsted,08:00:00,55.44648, 11.78757","Niels,Fluebækvej 204, 4100, Ringsted,08:00:00,55.44295, 11.79137","Heldagsskolen Specialtilbud, Vestervej 27, 4100, Ringsted,55.44050, 11.78115"] 

我試圖

ss.Split('],['); 

但因爲這隻需單個字符,我不能分割的字符串。

+0

我已經有問題顯示它。我有一個字符串[[「我」],[「你」]]我想分開它作爲[「我」]和[「你」]。 –

回答

2

使用JavaScriptSerializer因爲您的字符串接近json。

var listOfLists = new JavaScriptSerializer().Deserialize <List<List<string>>>(str); 

,你會得到兩個列表各有3個項目作爲字符串的格式版本意味着

[ 
    [ 
    "Karoline,Ejlstrupvej 90 90, 4100, Ringsted,07:50:00,55.48148, 11.78890", 
    "Karoline,Ejlstrupvej 101, 4100, Ringsted,07:50:00,55.47705, 11.78523", 
    "Byskovskolen, Prstevej 19, 4100, Ringsted,55.46842, 11.80975" 
    ], 
    [ 
    "Mads,Sdr. Parkvej 27, 4100, Ringsted,08:00:00,55.44648, 11.78757", 
    "Niels,Fluebækvej 204, 4100, Ringsted,08:00:00,55.44295, 11.79137", 
    "Heldagsskolen Specialtilbud, Vestervej 27, 4100, Ringsted,55.44050, 11.78115" 
    ] 
] 
+0

@Downvoter有什麼理由? – I4V

+0

我們在框架4.5中有JavaScriptSerializer類嗎? –

+0

@vaibhavshah當然,我發佈這個答案之前,我測試了它與4.5。 'System.Web.Script.Serialization' – I4V

0

可以使用string.Split與串的陣列,因爲這樣的:

var things = thing.Split(
    new string[] { "],[" }, 
    StringSplitOptions.RemoveEmptyEntires 
); 

然後從各自的結果的龍頭[和尾隨]

試圖將一個字符串擰入字符字面值顯然是永遠不會工作的。

1
var res = ss.Split(new string[]{ "],[" }, StringSplitOptions.None); 
0
var pattern = @"\[\[|\]\]|\],\["; 
Regex r = new Regex(pattern); 
var splitList = r.Split(ss).Where(s => !string.IsNullOrEmpty(s)).ToList(); 
相關問題