Splitting a comma separated string

martin_d_bell

Newcomer
Joined
Oct 8, 2004
Messages
17
Hi Everyone,

I have a string in the form of

Blah1, Blah2, Blah3, Blah4, Blah5

I need to strip the Blah's and store them as separate string variables such as

strVar1 = Blah1
strVar2 = Blah2
strVar3 = Blah3
etc
etc

The string will be of different lengths and could contain 3 Blah's or 8 Blahs so the string length is never constant.

I get the feeling that a regular expression is the solution but haven't got a clue how to write one of these to do the job. If anyone could help it would be very much appreciated. Thanks in advance.

Martin
 
You could use the simple string.Split method
Visual Basic:
dim s as string = "Blah1, Blah2, Blah3, Blah4, Blah5" 'or whatever the string is
dim ans() as string = s.split(",")

or if you really require regular expressions then something like
Visual Basic:
Dim r as new RegEx("(,)")
Dim s as String = "Blah1, Blah2, Blah3, Blah4, Blah5"
dim ans() as String = r.Split(s)
should do it. may not be exact as I don't have VS on this PC.
 
Thanks

Thanks alot for your help.

I used the spit function in the end. Sometimes the answer is right under your nose but I manage to spend 2 hours over complicating things!! Oh well, thank you. ;)
 
Back
Top