SPONSORS:






User Tag List

Results 1 to 3 of 3
  1. #1
    Senior Member
    Join Date
    Nov 1999
    Location
    Eagle, ID,USA
    Posts
    1,035
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Converting from string to integer

    you can do the following:

    STRING sVal = "abcde123"

    while (IsAlpha(sVal))
    sVal = Stuff(sVal,1,1,"")

    INTEGER iVal = Val(sVal)
    print(iVal)
    //should print 123

    ------------------


    [This message has been edited by TonyM (edited 06-05-2002).]
    Have you ever noticed? Anybody going slower than you is an idiot, and anyone going faster than you is a maniac.

  2. #2
    Junior Member
    Join Date
    Jun 2002
    Posts
    12
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Converting from string to integer


    I can use val() to convert a string to an
    integer if the string was prefixed with
    the integer. However, if there were
    non-numeric chars in the string before
    the integer in the string - the usage
    of val () will fail and return 0.

    For example if I wanted to convert
    extract the integer 123 from the string
    "abcde123" .. what is the best and elegant
    way to do it.



    ------------------

  3. #3
    Senior Member
    Join Date
    Jul 2000
    Posts
    186
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Converting from string to integer

    If the alpha characters you want to get rid of can be anywhere in the string, as opposed to the beginning, try something like this:

    [-] main()
    [ ] print(sReturnJustNumbers("abc123"))
    [ ] print(sReturnJustNumbers("123abc"))
    [ ] print(sReturnJustNumbers("1a2b3c"))
    [ ]
    [-] string sReturnJustNumbers(string s)
    [ ] int i
    [-] for i = 1 to len(s)
    [-] if IsAlpha(substr(s,i,1))
    [ ] s = Stuff(s,i,1," ")
    [ ] s=StrTran(s," ","")
    [ ] return s


    ------------------
    Tom
    Tom

 

 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

vBulletin Optimisation provided by vB Optimise v2.6.0 Beta 4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
User Alert System provided by Advanced User Tagging v3.0.9 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Questions / Answers Form provided by vBAnswers (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
vBNominatevBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Feedback Buttons provided by Advanced Post Thanks / Like (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Username Changing provided by Username Change (Free) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
BetaSoft Inc.
Digital Point modules: Sphinx-based search
All times are GMT -8. The time now is 11:01 PM.

Copyright BetaSoft Inc.