Jump to content
Xtreme .Net Talk

Recommended Posts

  • *Experts*
Posted

In the never-ending quest to ditch the old VB6 way of doing

things, I'm 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 I'm 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.NET's features. :)

 

So how can I use String.Split and pass another String as a delimeter?

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

  • Leaders
Posted

Can you post what your trying? I just had a crack at it and it seems to work with one quirk.

 

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.

--tim
Posted

There is one way of doing this, the result is correct, but I don't know if it's a "clean" enough solution:

 

 

Dim sTest1 As String = "Good****morning****afternoon****and night"

Dim y As String() = sTest1.Replace("****", Chr(0)).Split(Chr(0))

  • *Experts*
Posted

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"

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

  • *Experts*
Posted

I'll 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 I'll probably stick

in a function and use it. :) Thanks.[/edit]

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Posted

In Java there's a nice class named StringTokenizer. To be honest, I'm surprised that .NET doesn't 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 I'd go ahead and do it myself. :-\

 

http://java.sun.com/j2se/1.4.1/docs/api/java/util/StringTokenizer.html

Gamer extraordinaire. Programmer wannabe.
Posted

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.

 

'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

Gamer extraordinaire. Programmer wannabe.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...