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

bostonsteAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Jorge PaulinoConnect With a Mentor 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))

Open in new window

0
 
zhubaCommented:
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
 
Jaime OlivaresSoftware 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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
zhubaCommented:
I'd forgotten about vb's isNumeric function, that's probably the best choice for what you're trying to do.
0
 
bostonsteAuthor Commented:
Many thanks
0
 
Jorge PaulinoIT 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))

Open in new window

0
All Courses

From novice to tech pro — start learning today.