tags:

views:

240

answers:

3

How do I split a string with a string in C# .net 1.1.4322?

String example:

Key|Value|||Key|Value|||Key|Value|||Key|Value

need:

Key|Value
Key|Value
Key|Value
  • I cannot use the RegEx.Split because the separating character is the ||| and just get every character separately.

  • I cannot use the String.Split() overload as its not in .net 1.1

Example of Accepted solution:

using System.Text.RegularExpressions;

String[] values = Regex.Split(stringToSplit,"\\|\\|\\|");
+5  A: 

What about using @"\|\|\|" in your Regex.Split call? That makes the | characters literal characters.

John JJ Curtis
Worked perfectly. Thanks.
dilbert789
+3  A: 

One workaround is replace and split:

string[] keyvalues = "key|value|||key|value".replace("|||", "~").split('~');
Joel Potter
And what happens if the original string has a `~` in it somewhere?
LukeH
`~` is just an example. You could use any character, but this certainly is not a perfect workaround.
Joel Potter
A: 

here is an example:

System.Collections.Hashtable table;
string[] items = somestring.split("|||");
foreach(string item in items)
{
   string[] keyvalue = item.split("|");
   table.add(keyvalue[0],keyvalue[1]);
}
Athens
`Split(string, StringCompareOptions)` and `Split(string, Int32, StringCompareOptions)` didn't appear until .NET Framework 2.0. OP specified 1.1
Kev