Manipulating a string

Posted on 2008-06-25
Last Modified: 2010-04-30
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

Question by:bostonste

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

Accepted Solution

jpaulino earned 500 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
                myString &= c
            End If
        Debug.Print(String.Format("Number:{0}, String:{1}", myNumber, myString))

Open in new window

LVL 55

Expert Comment

by:Jaime Olivares
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)
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.


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.

Author Closing Comment

ID: 31470812
Many thanks
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))

Open in new window


Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question