getting Nth position in a comma delimited string..

H

hazz

yes i should know this but ...

V,-1,1,2,-1,333,5

I would like to return the value from any position within the comma
delimited string

position 1 is "v"
position 2 is "-1"
position 6 is "333"

at first I thought I could use string.chars(n) until I realized that
1. There are commas and that the spaces between the commas vary so I have to
use an algorithm and function(s) that look for
1. the first item contained before the first comma,
2 the second item contained between the first and second comma,
3 the third item contained between the second and third,
etc.

thank you,
-hazz
 
S

Shane Story

hazz,

Why not Split them and use trim on the strings?

Dim s As String = "a, b, c, d"
MsgBox(s.Split(",")(1).Trim)

HTH,

Shane Story
 
G

Guest

hazz,

Perhaps you could use a variation of:

Dim myArray() As String = Split("V,-1,1,2,-1,333,5", ",")

For Each s As String In myArray
MsgBox(s)
Next

Kerry Moorman
 
C

Cathal Connolly [VB MVP]

You can just use the build in split() function i.e.
Dim myString As String = "V,-1,1,2,-1,333,5"
Dim myArray() As String = Split(myString,",")

or else use one of the overloads of the String.Split or RegEx.Split methods.
 
H

hazz

Thank you!

Shane Story said:
hazz,

Why not Split them and use trim on the strings?

Dim s As String = "a, b, c, d"
MsgBox(s.Split(",")(1).Trim)

HTH,

Shane Story
 
H

hazz

Thank you!

Kerry Moorman said:
hazz,

Perhaps you could use a variation of:

Dim myArray() As String = Split("V,-1,1,2,-1,333,5", ",")

For Each s As String In myArray
MsgBox(s)
Next

Kerry Moorman
 
H

hazz

Cathal Connolly said:
You can just use the build in split() function i.e.
Dim myString As String = "V,-1,1,2,-1,333,5"
Dim myArray() As String = Split(myString,",")

or else use one of the overloads of the String.Split or RegEx.Split
methods.
 
H

hazz

Thank you!

Cathal Connolly said:
You can just use the build in split() function i.e.
Dim myString As String = "V,-1,1,2,-1,333,5"
Dim myArray() As String = Split(myString,",")

or else use one of the overloads of the String.Split or RegEx.Split
methods.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top