Delimiter Split

  • Thread starter Thread starter Mark Fox
  • Start date Start date
M

Mark Fox

Hello,

The string.Split method is very useful for splitting
strings that use a single character as a delimiter. i.e.:

string str = "First;Second;Third";

string[] strArray = str.Split(new char[] {';'});

But I am having trouble getting the Split method to split
a string with a delimiter of more than one character.
What is the correct syntax?

string str = "First<;>Second<;>Third";

// This does not work, it gives an "Unhandled Exception
// of type 'System.ExecutionEngineException' in
// mscorlib.dll"
string[] strArray = str.Split(new char[] {'<',';','>'});

Thanks!
 
Hello,

The string.Split method is very useful for splitting
strings that use a single character as a delimiter. i.e.:

string str = "First;Second;Third";

string[] strArray = str.Split(new char[] {';'});

But I am having trouble getting the Split method to split
a string with a delimiter of more than one character.
What is the correct syntax?

string str = "First<;>Second<;>Third";

// This does not work, it gives an "Unhandled Exception
// of type 'System.ExecutionEngineException' in
// mscorlib.dll"
string[] strArray = str.Split(new char[] {'<',';','>'});

Mark,

To split on multiple characters, use the Regex.Split method:

using System.Text.RegularExpressions;

...

string str = "First<;>Second<;>Third";
string[] strArray = Regex.Split(str, "<;>");


Hope this helps.

Chris.
 
Back
Top