Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 151
  • Last Modified:

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

0
bostonste
Asked:
bostonste
1 Solution
 
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
 
jpaulinoCommented:
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
 
Jaime OlivaresCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
jpaulinoCommented:
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now