# Manipulating a string

Hello all

I have a string that looks like this  128050GWSOP. Is it possible to break this down into 2 variable2, one containing all the numbers the other containing all the letters. Not able to just count characters as it is not alwas the same, have to read characters and decide if a number or not

Many thanks

###### Who is Participating?

IT Pro/DeveloperCommented:
Hi bostonste,

Try something like this:
``````        Dim str As String = "128050GWSOP"
Dim myNumber As String = Nothing
Dim myString As String = Nothing
For Each c As Char In str
If IsNumeric(c) Then
myNumber &= c
Else
myString &= c
End If
Next
Debug.Print(String.Format("Number:{0}, String:{1}", myNumber, myString))
``````
0

Commented:
If it is stored as an ascii string then the numbers 0-9 are the hex values 0x30 to 0x39 and the uppercase letters are the hex values 0x41 to 0x5A so if you have two if statements and compare them within those ranges then you can determine which variable to add them into.
0

Software ArchitectCommented:
if your main goal is to determine if the string contains a valid number, you can use TryParse, like:

Dim s as String = "128050GWSOP"
Dim isNumber as Boolean
Dim number as Int64

isNumber = [Int64].TryParse(yourString, number)
0

Commented:
I'd forgotten about vb's isNumeric function, that's probably the best choice for what you're trying to do.
0

Author Commented:
Many thanks
0

IT Pro/DeveloperCommented:
Here's a easy way to do it:
``````        Dim str As String = "128050GWSOP"

Dim myNumber As String = Val(str)
Dim myString As String = str.Replace(myNumber, String.Empty)

Debug.Print(String.Format("Number:{0}, String:{1}", myNumber, myString))
``````
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.