# Solved: VB String Parsing



## html81993 (Jul 8, 2005)

I was wondering if there was a method that is similar to the Java (java.util.)StringTokenizer.

I have a string in VB seperated with a colon. I am able to place the first part (before the colon) into a variable. How would I get the second part of it?


----------



## pvc_ (Feb 18, 2008)

There are a number of ways to do this. The first method is easier, but is not recommended. Use it when there are multiple tokens (multiple colons):

dim Splitter(), Str as string

Str = "Hello:World"
Splitter = Str.split(":")

Each element of the array, Splitter would have a token. ex. Splitter(0), Splitter(1)...

The second method would use Instr() to get the position of the char that you are looking for, and then uses mid() to parse it out.

dim PosX as integer
PosX = InStr("Hello:World", ":")
Token = mid(Str, 6)


----------



## html81993 (Jul 8, 2005)

In the second method, Instr() (this is based on my understanding)

posX = 6
Token = Hello:
Is that correct?


----------



## OBP (Mar 8, 2005)

I am not a VB6 programmer, only VBA.
But I would use instr to find the position of the colon and then use left and right to extract the remaining data.
i.e.
dim posx as integer, data as string, leftbit as string, rightbit as string
data = your string
posx = instr(data, ":")
leftbit = left(data, posx -1)
rightbit = right( data, (len(data) - posx))


----------



## pvc_ (Feb 18, 2008)

yes, actually you don't need that 6, instead use the PosX variable. like: mid(Str, PosX).

try not to use left() or right() because I don't think they are supported in .net and the mid() function does the same thing.


----------



## OBP (Mar 8, 2005)

pvc, I don't think you have the correct syntax in your example, shouldn't it be

mid(string, startposition, lengthofpart)

where string is the data string, startposition is the start of the part that you want and lengthofpart is the length of the part of the string that you want.
or is your VB different?


----------



## pvc_ (Feb 18, 2008)

I just wrote it off top of my head, but if you don't define the lengthofpart, it would go all the way to the end of the string. It's good for when you don't know the length.


----------



## OBP (Mar 8, 2005)

pvc, I just checked and it does the same in VBA as well, we live & learn


----------



## html81993 (Jul 8, 2005)

Thanks everyone. I ended up using Splitter. It works great. Thanks.


----------

