By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,953 Members | 1,562 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,953 IT Pros & Developers. It's quick & easy.

Split a string

P: n/a
There is a function somewhere to split a string with multiple tokens at a
time?

Say I have this:

aaaa#bbbbb*ccccc$dddd

I whould like to split it so the result whould be
aaaa
bbb
cccc
dddd

in a single call with array of delimiters #,*,&

--
Ceers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------
Nov 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Cor
Hi Crirus,

I have just copied this from MSDN, I saw it yesterday while busy with that
nice problem with the double and more space string split.

Public Shared Sub Main()

Dim delimStr As String = " ,.:"
Dim delimiter As Char() = delimStr.ToCharArray()
Dim words As String = "one two,three:four."
Dim split As String() = Nothing

Console.WriteLine("The delimiters are -{0}-", delimStr)
Dim x As Integer
For x = 1 To 5
split = words.Split(delimiter, x)
Console.WriteLine(ControlChars.Cr + "count = {0,2} ..............",
x)
Dim s As String
For Each s In split
Console.WriteLine("-{0}-", s)
Next s
Next x
End Sub 'Main
Nov 20 '05 #2

P: n/a
Hi Crirus,

|| There is a function somewhere to split a string
|| with multiple tokens at a time?

LOL. Guess what? The function's called Split!!

|| in a single call with array of delimiters #,*,&

and it takes a (param) array of delimiters. ROFL.

"aaaa#bbbbb*ccccc$dddd".Split ("#"c, "*"c, "$"c) As String()

Sorry, I shouldn't laugh! ;-))

There was a long thread all about splitting only yesterday
Topic: Space delimiter, dated 5th Nov

Regards,
Fergus
Nov 20 '05 #3

P: n/a
Crirus,
In addition to the others comments, here is my short String Split FAQ:

---x--- cut here ---x---
There are three Split functions in VB.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.

Alternatively use System.Text.RegularExpressions.RegEx.Split to split based
on matching patterns.

---x--- cut here ---x---

As the others have demonstrated you want the second one.

Dim input As String = "aaaa#bbbbb*ccccc$dddd"
Dim values() As String

values = input.Split("#"c, "*"c, "$"c)

Note the String.Split method accepts a ParamArray of characters you can list
the chars as Fergus & I showed or you can use String.ToCharArray as Cor
demonstrated. Both are useful.

The c in "#"c indicates that the pound sign is a Char literal instead of a
String literal. Important to know if you are using Option Strict On.

Hope this helps
Jay
"Crirus" <Cr****@datagroup.ro> wrote in message
news:uP**************@TK2MSFTNGP12.phx.gbl...
There is a function somewhere to split a string with multiple tokens at a
time?

Say I have this:

aaaa#bbbbb*ccccc$dddd

I whould like to split it so the result whould be
aaaa
bbb
cccc
dddd

in a single call with array of delimiters #,*,&

--
Ceers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------

Nov 20 '05 #4

P: n/a
OK, thanks to all...

--
Ceers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------

"Crirus" <Cr****@datagroup.ro> wrote in message
news:uP**************@TK2MSFTNGP12.phx.gbl...
There is a function somewhere to split a string with multiple tokens at a
time?

Say I have this:

aaaa#bbbbb*ccccc$dddd

I whould like to split it so the result whould be
aaaa
bbb
cccc
dddd

in a single call with array of delimiters #,*,&

--
Ceers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------

Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.