How do I Convert a String to an Integer when the string is prefixed with a character(s)

Hi,

I am trying to do the above, i.e.

   "ABC123"   -->  123
   "X12"    -->  12

Any ideas???

Thanks

James
LVL 4
James AtkinSenior Principle Software EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DaveCommented:
Hi jatkin,

You could use a RegExp such as

Sub GetDigits()
    Dim Mystr As String, RegEx As Object
    Set RegEx = CreateObject("vbscript.regexp")
    Mystr = "ABC123" & vbNewLine & "X12"
    RegEx.Pattern = "[\d+]"
    RegEx.Global = True
    MsgBox RegEx.Replace(Mystr, "")
End Sub

Cheers

Dave
0
James AtkinSenior Principle Software EngineerAuthor Commented:
Interesting solution...

The example code appears to return the part I need to throw away ;-)  i.e.  
   ABC
   X

Haven't dealt with Regular Expressions since the days of Lexx & Yacc...
What is the 'inverse' of [\d+]?

James
0
DaveCommented:
I'm a dope....left out the negating character

RegEx.Pattern = "[^\d+]"

Cheers

Dave
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
James AtkinSenior Principle Software EngineerAuthor Commented:
That's the one!

Thanks for the help....

James
0
DaveCommented:
Thanks for the grade James  :)

The RegEx I gave is a global one so it will convert an entire string
If you had a string such as
"ABC123 X12"
and you only wanted to pull out the first group of non numbers then remove the line
RegEx.Global = True

and I should have written the pattern as
"[^\d]+"

Cheers

Dave
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

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.