-3

在我的 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('],[');

但由于这只需要单个字符,我无法拆分字符串。

4

4 回答 4

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"
  ]
]
于 2013-04-18T09:15:39.450 回答
1
var res = ss.Split(new string[]{ "],[" }, StringSplitOptions.None);
于 2013-04-18T09:15:36.297 回答
0

您可以使用string.Split字符串数组,例如:

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

然后从各自的结果中删除前导[和尾随。]

试图将字符串硬塞到字符文字中显然是行不通的。

于 2013-04-18T09:14:48.073 回答
0
var pattern = @"\[\[|\]\]|\],\[";
Regex r = new Regex(pattern);
var splitList = r.Split(ss).Where(s => !string.IsNullOrEmpty(s)).ToList();
于 2013-04-18T09:39:16.607 回答