Connecting Tech Pros Worldwide Help | Site Map

problem with data.Split(vbCrLf)

  #1  
Old November 21st, 2005, 10:04 AM
Ron
Guest
 
Posts: n/a
Hello,

I am trying to parse a string on the newline char. I
guess vbCrLf is a string constant. How can I parse my
string - data - on the newline char?
....
data += ASCII.GetString(buffer, 0, bytesRead)
....
Dim parts() As String = data.Split(vbCrLf)

Thanks,
Ron
  #2  
Old November 21st, 2005, 10:04 AM
Alex Clark
Guest
 
Posts: n/a

re: problem with data.Split(vbCrLf)


Hi Ron,

I think the Split method of the string object either takes a single char or
an array of chars. If you use the latter overload, .NET will assume that
any of the chars in the array qualify as a separating character, hence if
you pass in CrLf it decides that either Cr OR Lf is a separator. This means
you'll end up with a blank string for every other element in the array of
parts().

The way I always use is the good ol' fashioned VB Split function, as this
does exactly what you want, eg:

Dim parts() As String = Split(data, vbCrLf)

Regards,
Alex Clark


"Ron" <anonymous@discussions.microsoft.com> wrote in message
news:171201c4f4f2$d601ed50$a601280a@phx.gbl...[color=blue]
> Hello,
>
> I am trying to parse a string on the newline char. I
> guess vbCrLf is a string constant. How can I parse my
> string - data - on the newline char?
> ...
> data += ASCII.GetString(buffer, 0, bytesRead)
> ...
> Dim parts() As String = data.Split(vbCrLf)
>
> Thanks,
> Ron[/color]


  #3  
Old November 21st, 2005, 10:04 AM
Chris
Guest
 
Posts: n/a

re: problem with data.Split(vbCrLf)


You can use a StringReader to read a string line by line:

Dim sr As new StringReader(sStringToRead)
while sr.peek <> -1
Dim s as string = sr.ReadLine
'Do something with s here
end While

Perhaps this will help you

  #4  
Old November 21st, 2005, 10:05 AM
Ron
Guest
 
Posts: n/a

re: problem with data.Split(vbCrLf)


Thanks. I thought about that, but I thought I would check
to see if maybe I was missing something in vb.net. Guess
I have to import the Microsoft.VisualBasice namespace?

Thanks again,
Ron

[color=blue]
>-----Original Message-----
>Hi Ron,
>
>I think the Split method of the string object either[/color]
takes a single char or[color=blue]
>an array of chars. If you use the latter overload, .NET[/color]
will assume that[color=blue]
>any of the chars in the array qualify as a separating[/color]
character, hence if[color=blue]
>you pass in CrLf it decides that either Cr OR Lf is a[/color]
separator. This means[color=blue]
>you'll end up with a blank string for every other element[/color]
in the array of[color=blue]
>parts().
>
>The way I always use is the good ol' fashioned VB Split[/color]
function, as this[color=blue]
>does exactly what you want, eg:
>
>Dim parts() As String = Split(data, vbCrLf)
>
>Regards,
>Alex Clark
>
>
>"Ron" <anonymous@discussions.microsoft.com> wrote in[/color]
message[color=blue]
>news:171201c4f4f2$d601ed50$a601280a@phx.gbl...[color=green]
>> Hello,
>>
>> I am trying to parse a string on the newline char. I
>> guess vbCrLf is a string constant. How can I parse my
>> string - data - on the newline char?
>> ...
>> data += ASCII.GetString(buffer, 0, bytesRead)
>> ...
>> Dim parts() As String = data.Split(vbCrLf)
>>
>> Thanks,
>> Ron[/color]
>
>
>.
>[/color]
  #5  
Old November 21st, 2005, 10:05 AM
Ron
Guest
 
Posts: n/a

re: problem with data.Split(vbCrLf)


It took me a while to see what you were doing. This is
pretty cool. I will give that a try. I am trying to
steer away from using Microsoft.VisualBasic for the sake
of using DotNet functionality. I will give this a try,
otherwise, I will go with the good'ol VB Split function.

Thanks,
Ron

[color=blue]
>-----Original Message-----
>You can use a StringReader to read a string line by line:
>
>Dim sr As new StringReader(sStringToRead)
>while sr.peek <> -1
>Dim s as string = sr.ReadLine
>'Do something with s here
>end While
>
>Perhaps this will help you
>
>.
>[/color]
  #6  
Old November 21st, 2005, 10:05 AM
Herfried K. Wagner [MVP]
Guest
 
Posts: n/a

re: problem with data.Split(vbCrLf)


"Ron" <anonymous@discussions.microsoft.com> schrieb:[color=blue]
> Thanks. I thought about that, but I thought I would check
> to see if maybe I was missing something in vb.net. Guess
> I have to import the Microsoft.VisualBasice namespace?[/color]

This should be done automatically in VB.NET projects. 'Split' is member of
the 'Microsoft.VisualBasic.Strings' module.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

  #7  
Old November 21st, 2005, 10:05 AM
Ron
Guest
 
Posts: n/a

re: problem with data.Split(vbCrLf)


Thanks. I thought about that, but I thought I would check
to see if maybe I was missing something in vb.net. Guess
I have to import the Microsoft.VisualBasice namespace?

Thanks again,
Ron

[color=blue]
>-----Original Message-----
>Hi Ron,
>
>I think the Split method of the string object either[/color]
takes a single char or[color=blue]
>an array of chars. If you use the latter overload, .NET[/color]
will assume that[color=blue]
>any of the chars in the array qualify as a separating[/color]
character, hence if[color=blue]
>you pass in CrLf it decides that either Cr OR Lf is a[/color]
separator. This means[color=blue]
>you'll end up with a blank string for every other element[/color]
in the array of[color=blue]
>parts().
>
>The way I always use is the good ol' fashioned VB Split[/color]
function, as this[color=blue]
>does exactly what you want, eg:
>
>Dim parts() As String = Split(data, vbCrLf)
>
>Regards,
>Alex Clark
>
>
>"Ron" <anonymous@discussions.microsoft.com> wrote in[/color]
message[color=blue]
>news:171201c4f4f2$d601ed50$a601280a@phx.gbl...[color=green]
>> Hello,
>>
>> I am trying to parse a string on the newline char. I
>> guess vbCrLf is a string constant. How can I parse my
>> string - data - on the newline char?
>> ...
>> data += ASCII.GetString(buffer, 0, bytesRead)
>> ...
>> Dim parts() As String = data.Split(vbCrLf)
>>
>> Thanks,
>> Ron[/color]
>
>
>.
>[/color]
  #8  
Old November 21st, 2005, 10:06 AM
Ron
Guest
 
Posts: n/a

re: problem with data.Split(vbCrLf)


It took me a while to see what you were doing. This is
pretty cool. I will give that a try. I am trying to
steer away from using Microsoft.VisualBasic for the sake
of using DotNet functionality. I will give this a try,
otherwise, I will go with the good'ol VB Split function.

Thanks,
Ron

[color=blue]
>-----Original Message-----
>You can use a StringReader to read a string line by line:
>
>Dim sr As new StringReader(sStringToRead)
>while sr.peek <> -1
>Dim s as string = sr.ReadLine
>'Do something with s here
>end While
>
>Perhaps this will help you
>
>.
>[/color]
  #9  
Old November 21st, 2005, 10:06 AM
Herfried K. Wagner [MVP]
Guest
 
Posts: n/a

re: problem with data.Split(vbCrLf)


"Ron" <anonymous@discussions.microsoft.com> schrieb:[color=blue]
> Thanks. I thought about that, but I thought I would check
> to see if maybe I was missing something in vb.net. Guess
> I have to import the Microsoft.VisualBasice namespace?[/color]

This should be done automatically in VB.NET projects. 'Split' is member of
the 'Microsoft.VisualBasic.Strings' module.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

  #10  
Old November 21st, 2005, 10:06 AM
Cor Ligthert
Guest
 
Posts: n/a

re: problem with data.Split(vbCrLf)


Alex,

Thanks for that, it is obvious, however I did never understand it.

Cor


  #11  
Old November 21st, 2005, 10:06 AM
Simon Verona
Guest
 
Posts: n/a

re: problem with data.Split(vbCrLf)


you could always remove the CR using string.remove(vbcr) and then split on
LF only - eg string.split(vblf).

ie
dim mystringarray() as string =mystring.remove(vbCr).split(vbLf)

Hope this helps..
Simon


"Ron" <anonymous@discussions.microsoft.com> wrote in message
news:121c01c4f509$720f69a0$a401280a@phx.gbl...[color=blue]
> It took me a while to see what you were doing. This is
> pretty cool. I will give that a try. I am trying to
> steer away from using Microsoft.VisualBasic for the sake
> of using DotNet functionality. I will give this a try,
> otherwise, I will go with the good'ol VB Split function.
>
> Thanks,
> Ron
>
>[color=green]
>>-----Original Message-----
>>You can use a StringReader to read a string line by line:
>>
>>Dim sr As new StringReader(sStringToRead)
>>while sr.peek <> -1
>>Dim s as string = sr.ReadLine
>>'Do something with s here
>>end While
>>
>>Perhaps this will help you
>>
>>.
>>[/color][/color]


  #12  
Old November 21st, 2005, 10:07 AM
Cor Ligthert
Guest
 
Posts: n/a

re: problem with data.Split(vbCrLf)


Alex,

Thanks for that, it is obvious, however I did never understand it.

Cor


  #13  
Old November 21st, 2005, 10:07 AM
Simon Verona
Guest
 
Posts: n/a

re: problem with data.Split(vbCrLf)


you could always remove the CR using string.remove(vbcr) and then split on
LF only - eg string.split(vblf).

ie
dim mystringarray() as string =mystring.remove(vbCr).split(vbLf)

Hope this helps..
Simon


"Ron" <anonymous@discussions.microsoft.com> wrote in message
news:121c01c4f509$720f69a0$a401280a@phx.gbl...[color=blue]
> It took me a while to see what you were doing. This is
> pretty cool. I will give that a try. I am trying to
> steer away from using Microsoft.VisualBasic for the sake
> of using DotNet functionality. I will give this a try,
> otherwise, I will go with the good'ol VB Split function.
>
> Thanks,
> Ron
>
>[color=green]
>>-----Original Message-----
>>You can use a StringReader to read a string line by line:
>>
>>Dim sr As new StringReader(sStringToRead)
>>while sr.peek <> -1
>>Dim s as string = sr.ReadLine
>>'Do something with s here
>>end While
>>
>>Perhaps this will help you
>>
>>.
>>[/color][/color]


  #14  
Old November 21st, 2005, 10:11 AM
Jay B. Harlow [MVP - Outlook]
Guest
 
Posts: n/a

re: problem with data.Split(vbCrLf)


Ron,
As Alex stated, String.Split splits based on individual characters. If you
want to split based on words I would recommend Strings.Split or RegEx.Split.

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.


NOTE: Microsoft.VisualBasic *is* "DotNet functionality"! There is little
real reason to avoid it altogether. Some classes, such as
Microsoft.VisualBasic.Collection, I avoid altogether as most of the time it
is TOO general. I try not to mix Microsoft.VisualBasic.Strings functions
with System.String methods that take or return indexes, as VB.Strings uses
base 1 indexes, while System.String uses base 0 indexes, and this mixing
could lead to obscure bugs... VB.Split is one method I will use as
System.String currently does not have an actual equivalent (VS.NET 2005, aka
Whidbey, due out later in 2005, does have a String.Split
http://msdn2.microsoft.com/library/tabh47cf.aspx method that works on
words).

Hope this helps
Jay


"Ron" <anonymous@discussions.microsoft.com> wrote in message
news:171201c4f4f2$d601ed50$a601280a@phx.gbl...[color=blue]
> Hello,
>
> I am trying to parse a string on the newline char. I
> guess vbCrLf is a string constant. How can I parse my
> string - data - on the newline char?
> ...
> data += ASCII.GetString(buffer, 0, bytesRead)
> ...
> Dim parts() As String = data.Split(vbCrLf)
>
> Thanks,
> Ron[/color]


  #15  
Old November 21st, 2005, 10:11 AM
Jay B. Harlow [MVP - Outlook]
Guest
 
Posts: n/a

re: problem with data.Split(vbCrLf)


Ron,
As Alex stated, String.Split splits based on individual characters. If you
want to split based on words I would recommend Strings.Split or RegEx.Split.

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.


NOTE: Microsoft.VisualBasic *is* "DotNet functionality"! There is little
real reason to avoid it altogether. Some classes, such as
Microsoft.VisualBasic.Collection, I avoid altogether as most of the time it
is TOO general. I try not to mix Microsoft.VisualBasic.Strings functions
with System.String methods that take or return indexes, as VB.Strings uses
base 1 indexes, while System.String uses base 0 indexes, and this mixing
could lead to obscure bugs... VB.Split is one method I will use as
System.String currently does not have an actual equivalent (VS.NET 2005, aka
Whidbey, due out later in 2005, does have a String.Split
http://msdn2.microsoft.com/library/tabh47cf.aspx method that works on
words).

Hope this helps
Jay


"Ron" <anonymous@discussions.microsoft.com> wrote in message
news:171201c4f4f2$d601ed50$a601280a@phx.gbl...[color=blue]
> Hello,
>
> I am trying to parse a string on the newline char. I
> guess vbCrLf is a string constant. How can I parse my
> string - data - on the newline char?
> ...
> data += ASCII.GetString(buffer, 0, bytesRead)
> ...
> Dim parts() As String = data.Split(vbCrLf)
>
> Thanks,
> Ron[/color]


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regex.Split... Can I do this?? Jordi Rico answers 7 October 17th, 2006 10:35 PM
Split() function gives type mismatch Alan Silver answers 7 July 19th, 2005 03:29 PM