split function

hazejean

Regular
Joined
Jul 11, 2003
Messages
68
I have a text string with : in the middle
I want text left of the colon to go in one variable
I want text to right of colon to go to another variable

Thanks
 
You use the Split function of the String class to split the string into an array using a delimiter.
Visual Basic:
Dim myString As String = "leftside:rightside"
Dim parts() As String

parts = myString.Split(":")
At that point, parts(0) will contain "leftside" and parts(1) will contain "rightside".
 
Visual Basic:
Dim text As string = "Some : Text"
Dim texts() As String = text.Split(":", 2) 'split the text with ":" and then you can specify how many string can be returned
As the Split function an array of string, if you dont want array you will have to assign the values of the array members to your variables.
 
For i = 1 To noverbs
verb(i) = ListBox1.Items(i)
For j = 0 To 1
parts = verb(i).Split(" : ")
verbl(i) = parts(0)
verbr(i) = parts(1)
Next j
Next i
 
Well, the loop 'j' is completely redundant. I think the problem might be that you are splitting on " : " and not ":". Try this:
Visual Basic:
For i = 1 to noverbs
  verb(i) = ListBox1.Items(i)
  parts = verb(i).Split(":") 'note there's no spaces
  verbl(i) = parts(0)
  verbr(i) = parts(1)
Next i
 
I really have a more complex problem than above If there is a colon in the text string.. left side goes to verbl and right side goes to verbr.
But if no colon in line , whole string goes to verbr and verbl

but don't even have split working yet.

Thanks for any ideas.
 
This is a sample of questionaire:
Text left of : is checkbox label
Text to right of : goes to an output file if box is checked


another’s house : at another’s house
bar/club : in a bar or club
concert : at a concert
construction site : at a construction site
day care : in day care
home : at home
nursing home : in the resident’s nursing home
outdoors : in an outdoor venue
party : at a party
public place : in a public place
restaurant : in a restaurant
school : at school
store : in a store
work : at work
in this exapmle in parts(0) I get "work" "store" etc which is fine
when parts(1) is encountered gives out of range error
 
Back
Top