Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

String Extraction

Hi All,
This is probably a noob question but what I want to do is this.
I have a strings like "1,hello,765,Joe,345.67,blah"
I want to extract each part to a separate text box.
Like this:
Text1.text would have "1"
Text2.text would have "hello"
Text3.text would have "765"
etc.
Any help would be greatly appreciated. I hope this is not too dumb a question.
BTW: I have VB5. Can't afford the newest version yet! :D
Thanx in advance,
Stuw
[489 byte] By [stuw] at [2007-11-11 7:37:32]
# 1 Re: String Extraction
You can use the Split function from here: http://support.microsoft.com/default.aspx?scid=kb;en-us;188007 to split a comma-delimited string into an array of items. Then assign the value of each array item to the desired textbox, e.g.:

Dim Items() As String
Dim sInput As String
sInput = "1,hello,765,Joe,345.67,blah"
Items = Split(sInput, ",")
Text1.Text = Items(0)
Text2.Text = Items(1)
Text3.Text = Items(2)
' etc.
Phil Weber at 2007-11-11 17:26:46 >
# 2 Re: String Extraction
Hi Phil,
Thanx for the quick reply. I went to MS and put the code in as shown. I then added a cmd button to test it. When I click on the cmd button the code is what you gave me.
sInput = "1,hello,765,Joe,345.67,blah"
Items = Split(sInput, ",")
Text1.Text = Items(0)
Text2.Text = Items(1)
Text3.Text = Items(2)
but when I run it I get a Compile error, "Can't assign to array" and it hilights "Items =". I'm probably leaving something out huh?
Stuw
stuw at 2007-11-11 17:27:45 >
# 3 Re: String Extraction
Hi Again Phil,
I changed the "Dim Items() As String" to "Dim Items as Variant" and it seems to work.
Thanks so much for your efforts.
I love this site.
Stuw
stuw at 2007-11-11 17:28:46 >