-3

In my c# program I have one string like this.

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"]];

How can I separate the values like this.

  ["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"]

I was trying

ss.Split('],[');

But as this only takes single character, I am not able to split the strings.

afuzzyllama
  • 6,538
  • 5
  • 47
  • 64
vaibhav shah
  • 4,939
  • 19
  • 58
  • 96

4 Answers4

2

Use JavaScriptSerializer since your string is close to json.

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

And you'll get two lists each having 3 items as your string's formatted version implies

[
  [
    "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"
  ]
]
I4V
  • 34,891
  • 6
  • 67
  • 79
1
var res = ss.Split(new string[]{ "],[" }, StringSplitOptions.None);
Cyril Gandon
  • 16,830
  • 14
  • 78
  • 122
0

You can use string.Split with an array of string, as such:

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

Then remove the leading [ and trailing ] from the respective results.

Trying to shoehorn a string into a character literal is obviously never going to work.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
0
var pattern = @"\[\[|\]\]|\],\[";
Regex r = new Regex(pattern);
var splitList = r.Split(ss).Where(s => !string.IsNullOrEmpty(s)).ToList();
paul
  • 21,653
  • 1
  • 53
  • 54