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?
 
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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
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.

All Courses

From novice to tech pro — start learning today.