• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Visual LISP, AutoLISP and General Customization

    Reply
    Valued Contributor
    Posts: 95
    Registered: ‎01-24-2006

    Test String for First Character

    65 Views, 7 Replies
    03-26-2009 05:14 PM
    How can I test a string to see if the first character is a number between 0 and 9 or something else. I.E. A-Z or a-z or a symbol.
    Please use plain text.
    Mentor
    Posts: 289
    Registered: ‎03-15-2007

    Re: Test String for First Character

    03-26-2009 06:47 PM in reply to: mpeace
    Maybe this?

    (setq aa "1230C")

    (substr aa 1 1)
    "1"

    (setq aa "A230C")

    (substr aa 1 1)
    "A"


    HTH
    Southie
    Please use plain text.
    Valued Contributor
    Posts: 95
    Registered: ‎01-24-2006

    Re: Test String for First Character

    03-26-2009 06:59 PM in reply to: mpeace
    I'm not sure that's quite what I need.

    I'm looking for a way to test a string and depending on what the first character is run one of two sub routines. If the string starts with any thing other than a number from 0-9 run sub "A". If it starts with a number from 0-9 run sub "B".

    I'm trying to read text and pass it to my lisp. Some of the text will read "TC ###.##" and some will read "###.## TC". I want to end up with only the number, no alpha characters.
    Please use plain text.
    Distinguished Contributor
    Posts: 111
    Registered: ‎07-05-2007

    Re: Test String for First Character

    03-26-2009 07:51 PM in reply to: mpeace
    Hi,

    Try this utilising wcmatch.

    {code}
    (if (= (wcmatch (substr "TC 123.45" 1 1) "[0-9]") T)
    (progn

    ... do stuff here for sub routine A

    ) ; end progn
    (progn

    ... do stuff here for sub routine B

    ) ; end progn
    ) ; end if
    {code}

    Morgan.
    Please use plain text.
    *Jason Piercey

    Re: Test String for First Character

    03-27-2009 06:14 AM in reply to: mpeace
    You can use wcmatch as pointed out, but you do not need substr.

    [code]

    (cond
    ((wcmatch myString "@*") (alert "string starts with a letter"))

    ((wcmatch myString "#*") (alert "string starts with a number"))

    (t (alert "string starts with a nonalphanumeric character"))

    )

    [/code]

    wrote in message
    news:6150385@discussion.autodesk.com...
    How can I test a string to see if the first character is a number between 0
    and 9 or something else. I.E. A-Z or a-z or a symbol.
    Please use plain text.
    *Pro
    scot-65
    Posts: 1,928
    Registered: ‎12-11-2003

    Re: Test String for First Character

    03-27-2009 02:44 PM in reply to: mpeace
    Please use plain text.
    Valued Contributor
    Posts: 95
    Registered: ‎01-24-2006

    Re: Test String for First Character

    03-27-2009 02:58 PM in reply to: mpeace
    Thanks Jason, I was able to use your code and make it work for my needs.
    Please use plain text.
    *Marc'Antonio Alessi

    Re: Test String for First Character

    03-28-2009 01:29 AM in reply to: mpeace
    Maybe this is enough:

    Comando: (atof "TC 123.45")
    0.0

    Comando: (atof "TC123.45")
    0.0

    Comando: (atof "123.45")
    123.45

    Comando: (atof "123.45TC")
    123.45

    Comando: (atof "123.45 TC")
    123.45


    So:

    (defun OnlyNumNoPrx (Str_In / NumOut)
    (if (zerop (setq NumOut (atof Str_In)))
    (atof (substr Str_In 4))
    NumOut
    )
    )

    Comando: (OnlyNumNoPrx "123.45 TC")
    123.45

    Comando: (OnlyNumNoPrx "TC 123.45")
    123.45

    --
    Marc'Antonio Alessi
    (vl-string-translate "1234567890" "ie@mst.lan" "499825513610716")
    http://xoomer.alice.it/alessi
    > 2D Parametric for 2000-2009 <
    (strcat "I like " (substr (ver) 8 4) "!")
    GPS: N45° 50' 16" E12° 22' 44"
    --
    Please use plain text.