如何在c#中将字符串与字符串分开

时间:2013-04-18 09:12:02

标签: c# string

在我的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 个答案:

答案 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"
  ]
]

答案 1 :(得分:1)

var res = ss.Split(new string[]{ "],[" }, StringSplitOptions.None);

答案 2 :(得分:0)

您可以将string.Split与字符串数组一起使用,如下所示:

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

然后从相应的结果中移除前导[并尾随]

尝试将字符串装入字符文字显然永远不会起作用。

答案 3 :(得分:0)

var pattern = @"\[\[|\]\]|\],\[";
Regex r = new Regex(pattern);
var splitList = r.Split(ss).Where(s => !string.IsNullOrEmpty(s)).ToList();