"Evil".Split()

Bucky

Well-known member
Joined
Dec 23, 2001
Messages
791
Location
East Coast
User Rank
*Expert*
In the never-ending quest to ditch the old VB6 way of doing
things, Im now trying to figure out how to use String.Split like
the old Split$ function.

Using Split$, a delimeter could be another String, not only one
character. The String.Split method, however, only wants to take
a Char as a delimeter. I tried converting a string into a Char
array, but that treated each and every Char as a delimeter. Oops.

Nothing is wrong with using Split$, and Im forced into using it for
now, but as I mentioned, the world would be a much better place
if we all got along and used VB.NETs features. :)

So how can I use String.Split and pass another String as a delimeter?
 
Code:
        Dim x As String = "I, like, cheese"
        Dim y As String() = x.Split(", ".ToCharArray)

Youll get "I", "", "like", "", "cheese"

Dont really know why is puts blanks in there like it does.
 
Can you post what your trying? I just had a crack at it and it seems to work with one quirk.

Code:
Dim delimiter As String
        delimiter = "***"

        Dim myString As String
        myString = "hello***world***again***trying"
        Dim strAry As String()

        strAry = myString.Split(delimiter.ToCharArray)
The only problem with what I have is that it splits into 10 elements instead of 4 but this is my first try.
 
There is one way of doing this, the result is correct, but I dont know if its a "clean" enough solution:


Dim sTest1 As String = "Good****morning****afternoon****and night"
Dim y As String() = sTest1.Replace("****", Chr(0)).Split(Chr(0))
 
Code:
Dim toSplit As String = "The[split]quick brown fox[split]jumped over the[split]lazy dog"

toSplit.Split("[split]".ToCharArray()) returns 26 array items, because
each character is counted as a delimeter, and if any one of those
characters is encountered, the string splits there (see below).

Should I just stick to Split$??? :)

(0): "The"
(1): ""
(2): ""
(3): ""
(4): ""
(5): ""
(6): ""
(7): "qu"
(8): "ck brown fox"
(9): ""
(10): ""
(11): ""
(12): ""
(13): ""
(14): ""
(15): "jum"
(16): "ed over "
(17): "he"
(18): ""
(19): ""
(20): ""
(21): ""
(22): ""
(23): ""
(24): ""
(25): "azy dog"
 
Ill consider it, although using Split$ is much more convenient (sp?)
for the moment.

Thanks for your ideas, everyone.

[edit]Actually, Vitaly, I really like your idea, and Ill probably stick
in a function and use it. :) Thanks.[/edit]
 
In Java theres a nice class named StringTokenizer. To be honest, Im surprised that .NET doesnt have anything similar which is why I bring it up (.NET seems to take a lot of elements from Java in terms of the assemblies available compared to Java packages).

I only bring this up incase you were going to build your own Split function. It may give you some ideas. If I knew more about .NET Id go ahead and do it myself. :-\

http://java.sun.com/j2se/1.4.1/docs/api/java/util/StringTokenizer.html
 
Ideally in .NET youd use Regular Expressions to do this sort of thing. They seem to have focussed the attention more towards these in the framework and examples I have seen. There is a lot of support for them.
 
Divil is right about the heavy support for Regular Expressions. I finally got a chance to learn about them (at least enough to start doing basing things). Splitting words using a delim is easy enough using RegEx. I wrote up a quick console application that demonstrates the ease of use.

Code:
To shorten code below that uses this assembly.
Imports System.Text.RegularExpressions

Module Module1
    Sub Main()
        Sentence to split.
        Dim str As String = "Some[*split*]sentence[*split*]to[*split*]split."

        Dim delim As String = "[*split*]"   Delim to use.
        delim = Regex.Escape(delim)         Be sure to add escape chars.

        Split the string using our delim.
        Dim splitStr As String() = Regex.Split(str, delim)
        Console.WriteLine(splitStr.Length)  Print out the number of words.

        Print out each word.
        Dim word As String
        For Each word In splitStr
            Console.WriteLine(word)
        Next

        Console.ReadLine()
    End Sub
End Module
 
Back
Top