Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Manipulating a string

Posted on 2008-06-25
Medium Priority
147 Views
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
Question by:bostonste
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 7

Expert Comment

ID: 21870858
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

LVL 48

Accepted Solution

jpaulino earned 2000 total points
ID: 21870859
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

LVL 55

Expert Comment

ID: 21870875
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

LVL 7

Expert Comment

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

Author Closing Comment

ID: 31470812
Many thanks
0

LVL 48

Expert Comment

ID: 21870892
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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
###### Suggested Courses
Course of the Month4 days, 10 hours left to enroll