Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Split function delimiter 2

Status
Not open for further replies.

rvBasic

Programmer
Oct 22, 2000
414
BE
Does the split function allow for multicharacter delimiters?

According to [link ms-help://MS.VSCC.v90/MS.msdnexpress.v90.en/dv_vbalr/html/7e33b047-48fc-493b-bb8d-7d1ac3f90038.htm]Ms-help[/help]:
Delimiter
Optional. Any single character used to identify substring limits. If Delimiter is omitted, the space character (" ") is assumed to be the delimiter.
only a one character length string can be used.
But some examples shown on the same page seem to imply that a string of characters can be used:

Code:
Split("Alice and Bob", " AND ") results in {"Alice and Bob"}
Split("Alice and Bob", " AND ", ,CompareMethod.Text) results in {"Alice", "Bob"}

I'm particularly interested in using split(string,vbCrLf) to split an input file in its variuous line constituents, but I do get some strange results. So, I wonder if vbCrLf can be used as a delimiter?

(also sorry for the link: does not seem to work as expected)




_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
I'm not sure why you are getting strange results, however have you considered using the System.IO.File.ReadAllLines() function when you are reading your input file to get the string array? This saves you from having to read then entire contents into a string and then parsing out then lines.
 
RiverGuy: No I didn't, I used StreamReader ReadToEnd [smile]

The problem seemed to be that the file ended with a line feed character Asc=10, and that the split function generated an extra blank line.

I'll try the ReadAllLines approach

BTW: what about the delimiter? one or more characters allowed?

_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
BTW: what about the delimiter? one or more characters allowed?

Yes, but like most of the functions in the .Net libraries, there are many options. Here's an example using more than one character.

Code:
        Dim s As String = "yesterday and today and tomorrow"
        For Each st As String In s.Split(New String() {"and"}, StringSplitOptions.RemoveEmptyEntries)
            MessageBox.Show(st)
        Next

I'm passing in a single-element string array as the delimiter. If you look at the documentation for the Split function, you'll notice it accepts either a string array or a char array.

You'll notice the following code doesn't work
Code:
        Dim s As String = "yesterday and today and tomorrow"
        For Each st As String In s.Split("and")
            MessageBox.Show(st)
        Next

However, if you supply a single character, it will work
Code:
        Dim s As String = "yesterday x today x tomorrow"
        For Each st As String In s.Split("x")
            MessageBox.Show(st)
        Next
 
[1] Can use String.Split method with Environment.NewLine like this.
[tt]
Dim t as String()
t=s.Split(new String(){Environment.NewLine}, StringSplitOptions.None)
[/tt]
[2] Or use Regex.Split method again with Environment.NewLine.
[tt]
t=Regex.Split(s, Environment.NewLine)
[/tt]
 
For the moment this is very confusing to me. Maybe that, given some more experience, I'll find this one day obviuous [ponder]

_________________________________
In theory, there is no difference between theory and practice. In practice, there is. [attributed to Yogi Berra]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top