string.split only accept char[] separator

  • Thread starter Thread starter Henry Chen
  • Start date Start date
H

Henry Chen

Hi,

I have a string that needs to be parsed into the string[]. The separator is
not char[]. It is something like " at ". With current string.Split function,
it doesn't work. Is there any exist functions like "Split" that I can use to
do the job? Any idea of the simpliest way to do the job?

Thanks in advance,
Henry
 
String.Split is designed to be pretty basic. You need to use something like
a regex here. For example, given the string

"one::two::three::four::five::six"

this expression:

\w+[^::]

will give you six matches, each with one number.

There's probably a better way (Regex object have a Split method as well),
but you get the idea.
 
Hi,

I have a string that needs to be parsed into the string[]. The
separator is not char[]. It is something like " at ". With
current string.Split function, it doesn't work. Is there any
exist functions like "Split" that I can use to do the job? Any
idea of the simpliest way to do the job?

Henry,

Use the Regex.Split function (found in the
System.Text.RegularExpressions namespace).

Hope this helps.

Chris.
 
See string.ToCharArray(). For performance and simplicity, I'd recommend
string.Split(...) over RegEx.

Scott
 
Henry,
There are three Split functions in .NET:

Use Microsoft.VisualBasic.Strings.Split if you need to split a string based
on a specific word (string). It is the Split function from VB6.

Use System.String.Split if you need to split a string based on a collection
of specific characters. Each individual character is its own delimiter.

Use System.Text.RegularExpressions.RegEx.Split to split based
on matching patterns.

If you are using C#, you can reference the Microsoft.VisualBasic.dll
assembly to use the first function.

Hope this helps
Jay
 
Back
Top