Delimiter Split

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!
 
C

Chris R. Timmons

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top