#1
  1. No Profile Picture
    Contributing User
    ASP Adventurer (500 - 999 posts)

    Join Date
    Feb 2008
    Posts
    790
    Rep Power
    30

    Extract just a number out of a string


    Not sure how to do this and don't think that isnumeric function will do this...

    How do I extract just a number out of a string that contains both text and numbers?

    For example....

    $10,000
    result would be: 10000

    $10,000 max. amount paid
    result would be: 10000

    Appreciate you help.
  2. #2
  3. Forever Learning User
    ASP Adventurer (500 - 999 posts)

    Join Date
    Mar 2005
    Location
    Arda
    Posts
    659
    Rep Power
    78
    Use CDbl().

    VBScript Referance for CDbl.

    - Olórin
    - Olórin


    If anything I have said makes sense, click on the "Scales" icon just above this post.
    What I would rather be doing!
  4. #3
  5. Moderator
    ASP Super Hero (2500 - 2999 posts)

    Join Date
    Dec 2006
    Location
    West London, UK
    Posts
    2,715
    Rep Power
    931
    I think this will give you the results your looking for...

    Code:
    <%
    Function RemChr(string, remove)
        Dim i, j, tmp, strOutput
        strOutput = ""
        for j = 1 to len(string)
            tmp = Mid(string, j, 1)
            for i = 1 to len(remove)
                tmp = replace( tmp, Mid(remove, i, 1), "")
                if len(tmp) = 0 then exit for
            next
            strOutput = strOutput & tmp
        next
        RemChr = strOutput
    End Function
    
    ' First remove numbers from string
    chrToRemove = remchr("$10,000 max. amount paid","0123456789")
    ' then use new string as characters to remove
    response.write remchr("$10,000 max. amount paid",chrToRemove) & "<br />"
    
    chrToRemove = remchr("$10,000","0123456789")
    Response.Write remchr("$10,000",chrToRemove)
    %>

    Comments on this post

    • phoenixaz agrees : Works perfect!
    • keep_it_simple agrees
    Hope this advice helps.

    If so please show your appreciation by adding reputation points (click the Give Rep button on the bottom bar of the post).

    - Post your code - Post your errors - Be clear - Be courteous -

    AND PLEASE...Finalise your thread with a solution or confirmation that the last advice worked or failed.

    Visit My ASP Free Members Club Profile
  6. #4
  7. Forever Learning User
    ASP Adventurer (500 - 999 posts)

    Join Date
    Mar 2005
    Location
    Arda
    Posts
    659
    Rep Power
    78
    Originally Posted by icoombs
    I think this will give you the results your looking for...

    Code:
    <%
    Function RemChr(string, remove)
        Dim i, j, tmp, strOutput
        strOutput = ""
        for j = 1 to len(string)
            tmp = Mid(string, j, 1)
            for i = 1 to len(remove)
                tmp = replace( tmp, Mid(remove, i, 1), "")
                if len(tmp) = 0 then exit for
            next
            strOutput = strOutput & tmp
        next
        RemChr = strOutput
    End Function
    
    ' First remove numbers from string
    chrToRemove = remchr("$10,000 max. amount paid","0123456789")
    ' then use new string as characters to remove
    response.write remchr("$10,000 max. amount paid",chrToRemove) & "<br />"
    
    chrToRemove = remchr("$10,000","0123456789")
    Response.Write remchr("$10,000",chrToRemove)
    %>
    Nice function!! Will definetly file this one away for the future.

    I mentioned CDbl() thinking he only needed $10,000.00 converted. If there is more to the string then the other alpha characters would have to be removed first.

    Cool!!
  8. #5
  9. No Profile Picture
    Contributing User
    ASP Adventurer (500 - 999 posts)

    Join Date
    Feb 2008
    Posts
    790
    Rep Power
    30
    Originally Posted by icoombs
    I think this will give you the results your looking for...

    Code:
    <%
    Function RemChr(string, remove)
        Dim i, j, tmp, strOutput
        strOutput = ""
        for j = 1 to len(string)
            tmp = Mid(string, j, 1)
            for i = 1 to len(remove)
                tmp = replace( tmp, Mid(remove, i, 1), "")
                if len(tmp) = 0 then exit for
            next
            strOutput = strOutput & tmp
        next
        RemChr = strOutput
    End Function
    
    ' First remove numbers from string
    chrToRemove = remchr("$10,000 max. amount paid","0123456789")
    ' then use new string as characters to remove
    response.write remchr("$10,000 max. amount paid",chrToRemove) & "<br />"
    
    chrToRemove = remchr("$10,000","0123456789")
    Response.Write remchr("$10,000",chrToRemove)
    %>
    Nicely done...you are brilliant!

    I couldn't have come up with that one! That works perfect and is exactly what I was looking for. Did lots of Googles trying to find the answer and couldn't find anything close to working.

    I appreciate your expertise and you taking the time to respond.

    Take care!
  10. #6
  11. KIS
    ASP Hero (2000 - 2499 posts)

    Join Date
    Jul 2007
    Location
    USA
    Posts
    2,099
    Rep Power
    1438
    glad you got a solution...just wanted to add that vbs has the RegExp (Regular Expressions)...although it may seem awkward at first (hmmm..ok...an understatement ...but w/ a little practice (hmmm...ok..a lot )...you can create very powerful functions w/ little coding


    ie: by changing the "pattern" parameter in this tiny function...you can return numbers only, upper case letters only, letters only...etc...etc...from the same function - dynamic



    regexp
    Code:
    <%
      Function ReturnPattern(sPattern, sString)
       Set oRegExp = New RegExp
    
       oRegExp.Global = True
       oRegExp.Pattern = sPattern
    
       ReturnPattern = oRegExp.Replace(sString, "")   
      End Function
    
    
    response.write ReturnPattern("[^A-Z]","abCDefGhIjKLmnOpQrsTuvWxYz!0123456789*&^%$#@")
    ' return letters that are uppercase only
    ' CDGIKLOQTWY
    
    response.write ReturnPattern("[^a-zA-Z]","abCDefGhIjKLmnOpQrsTuvWxYz!0123456789*&^%$#@")
    ' return letters only
    ' abCDefGhIjKLmnOpQrsTuvWxYz
    
    response.write ReturnPattern("[^0-9]","abCDefGhIjKLmnOpQrsTuvWxYz!0123456789*&^%$#@")
    ' return numbers only
    ' 0123456789
    %>

    Comments on this post

    • Olórin agrees : Good Stuff!!:thumbs:
    Last edited by keep_it_simple; August 30th, 2008 at 01:33 AM. Reason: made function more dynamic
    Please give respect to those that helped solve an issue by clicking on the reputation icon
  12. #7
  13. No Profile Picture
    Contributing User
    ASP Adventurer (500 - 999 posts)

    Join Date
    Feb 2008
    Posts
    790
    Rep Power
    30
    Thanks for the vbs solution. I will give it a try.

    Always good to have alternative approaches to a solution,

    Thanks!

Similar Threads

  1. Login Control VS2005
    By ceema in forum .NET Development
    Replies: 3
    Last Post: March 13th, 2007, 08:31 AM
  2. Month number to corresponding string
    By rosquared in forum ASP Development
    Replies: 2
    Last Post: August 22nd, 2006, 01:06 AM
  3. Input string was not in a correct format error in asp.net
    By ehsanking in forum .NET Development
    Replies: 5
    Last Post: May 13th, 2005, 11:34 AM
  4. Input string was not in a correct format.
    By flamesburn in forum .NET Development
    Replies: 1
    Last Post: March 23rd, 2004, 12:23 PM
  5. Converting String to Number
    By mbock11 in forum ASP Development
    Replies: 2
    Last Post: December 5th, 2003, 09:36 PM

IMN logo majestic logo threadwatch logo seochat tools logo