Question Splitting a String in an array Question

scotbuff

Member
Joined
Jun 9, 2010
Messages
5
Programming Experience
Beginner
I am trying to split a string into array. I know about split, but it seems to need a separator of some type. Suppose I am going to be working with input like the following.

8561369

and I want to split each character of that string into an array.

I tried something like this, but again Split seems to need a separator, I just keep getting the entire string in one entry of my array, not split.
VB.NET:
Dim values as String

'I am going to be populating this string variable via user input eventually, 'which is why I cannot just add spaces.
values = "8561369"

Dim numbers = values.Split()

Dim n as String

For Each n In numbers
    Console.WriteLine(n)
Next n

I am very new to VB.net and I am using Visual Basic 2008 Express Edition. Thanks in advance.
 
A String is already a Char array, just loop the chars in the string:
VB.NET:
Dim SomeString As String = "8561369"

For Each chr As Char in SomeString
    MessageBox.Show(chr.ToString)
Next chr
 
Char to Integer

Is it possible to convert the individual char into integers? Basically I am trying to pull each of those numbers individually and use them to index another array. I tried doing a Convert.ToInt on the chr variable but it seems to change it to an entirely different number. Perhaps an ASCII conversion thing I am guessing.

VB.NET:
Dim SomeString As String = "8561369"

For Each chr As Char in SomeString
    ' I want to convert chr to a integer.  Example being, if chr is 8, I want it to be the integer 8.
    MessageBox.Show(chr.ToString)
Next chr
 
You want the index of each char? or the integer value of the char if it's a number?
VB.NET:
Dim SomeString As String = "8561369"

For Counter As Integer = 0 to SomeString.Length - 1
    'Counter is the index
    MessageBox.Show(SomeString(Counter).ToString)
Next Counter

Dim IntValue As Integer
For Each chr As Char in SomeString
    If Char.IsNumber(chr) Then IntValue = Integer.Parse(chr.ToString) Else IntValue = -1I
    'IntValue is the char as a number
    MessageBox.Show(IntValue.ToString)
Next chr
 
Back
Top