-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.

1
  • I have shown it in question. I have one string [["me"],["you"]] & I want to separate it as ["me"] & ["you"]. Commented Apr 18, 2013 at 9:15

4 Answers 4

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"
  ]
]
Sign up to request clarification or add additional context in comments.

4 Comments

@Downvoter any reason?
Do we have JavaScriptSerializer class in framework 4.5 ?
@vaibhavshah of course, I tested it with 4.5 before I posted this answer. System.Web.Script.Serialization
I added System.Web.Extensions Then I got it.
1
var res = ss.Split(new string[]{ "],[" }, StringSplitOptions.None);

Comments

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.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.