Splitting string

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi

I have a multi-line address field which has each line separated by CRLF. How
can I split this field into individual strings using crlf as separator?

Thanks

Regards
 
John said:
I have a multi-line address field which has each line separated by CRLF.
How can I split this field into individual strings using crlf as
separator?

'Microsoft.VisualBasic.Strings.Split' + 'ControlChars.CrLf' is what you are
looking for.
 
Don't you mean?:

Dim result As String() = theString.Split(New String()
{Environment.NewLine})
 
Well, seeing as how the OP has a string with CRLF's in it and want's to
split it on CRLF, how is:

dim result As String() = theString.Split(Chr(13))

going to do that?.

It is going to split the string on CR and leave the LF character at the
beginning of the 2nd and subsequent elements of the resulting array.
 
John said:
I have a multi-line address field which has each line separated by CRLF. How
can I split this field into individual strings using crlf as separator?

For VB 2003, you /have/ to use

Microsoft.VisualBasic.Strings.Split( s, vbCrLf )

Dim x as String _
= "X,Y,Z".Replace( ".", vbCrlf )

? Microsoft.VisualBasic.Strings.Split( x, vbCrLf ).Length
3

The String.Split method /does not/ cater for multiple-character
delimiters, only working with lists of characters, each of which it
treats as a separate delimiter, leaving you with lots of blank entries
in the resulting array).

? String.Split( x, vbCr, vbLf ).Length
5

VB 2005 has a [more] sensible String.Split method that can handle
multiple-character delimiters.

? String.Split( x, New String() { vbCrLf } ).Length
3

HTH,
Phill W.
 
Yes, I see. I had thought that Chr(13) would get the entire CRLF.

But, in your suggestion, the "New String()" is not needed as the Split
method returns an implied string array anyway. So, it seems the best choice
is a combination of our suggestions:

Dim result As String() = theString.Split(Environment.NewLine)
 
That would have been cool, Scott, except that in their infinite wisdom, the
powers that be omitted a suitable overload that can take just a string as
the delimiter.

A single char works because the String.Split(char()) overload takes a
ParamArray which can be 1 char or many seperate char but if you feed it
String.Split(ChrW(13), Chr(10)) it wouldn't treat the 2 characters as a
combined entity. Instaed it would split on either CR or LF and still give
incorrect results for this case.

The first overload that takes a string, actually takes an Array of strings
(and not a ParamArray) and so the usage actually needs to be, (in this
case):

Dim result As String() = theString.Split(New String()
{Environment.NewLine}, StringSplitOptions.None)

or:

Dim result As String() = theString.Split(New String()
{Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)

Unfortunately, in my haste to point out your error, I still got it wrong in
my first post to this thread. (Consider my wrist slapped.)
 
John,

I assume that you have a multiline field where the line itself is broken by
CRLFCRLF.

What I do than is first replace all CRLFCRLF by an impossible
charachterrange and than make from the CRLF a csv seperator character
depending on your country proceded and ended with a ", than I change the
impossible charachter back to
"""CRLFCRLF""" this sets than the needed starting and ending " as well.

Cor
 
Back
Top