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,"\\|\\|\\|");
Cœur
  • 37,241
  • 25
  • 195
  • 267
Dilbert789
  • 864
  • 2
  • 14
  • 29

4 Answers4

4

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

jjxtra
  • 20,415
  • 16
  • 100
  • 140
3

One workaround is replace and split:

string[] keyvalues = "key|value|||key|value".replace("|||", "~").split('~');
Joel
  • 19,175
  • 2
  • 63
  • 83
0

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]);
}
Joel
  • 19,175
  • 2
  • 63
  • 83
Athens Holloway
  • 2,183
  • 3
  • 17
  • 26
  • `Split(string, StringCompareOptions)` and `Split(string, Int32, StringCompareOptions)` didn't appear until .NET Framework 2.0. OP specified 1.1 – Kev Feb 08 '10 at 22:23
0
string input = "Hi#*#Hello#*#i#*#Hate#*#My#*#......" ;
string[] delim = new string[] { "#*#" };
string[] results = input.split(delim , StringSplitOptions.None); 
DaveShaw
  • 52,123
  • 16
  • 112
  • 141
  • The question is about .NET 1.1. The overload of `string.Split` accepting a `string[]` has been added in .NET 2.0 – Kevin Gosse Oct 29 '12 at 12:27