Solved

Manipulating a string

Posted on 2008-06-25
6
141 Views
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

0
Comment
Question by:bostonste
6 Comments
 
LVL 7

Expert Comment

by:zhuba
Comment Utility
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

by:
jpaulino earned 500 total points
Comment Utility
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
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 7

Expert Comment

by:zhuba
Comment Utility
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

by:bostonste
Comment Utility
Many thanks
0
 
LVL 48

Expert Comment

by:jpaulino
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

743 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now