Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Test String for First Character

7 REPLIES 7
Reply
Message 1 of 8
mpeace
1079 Views, 7 Replies

Test String for First Character

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.
7 REPLIES 7
Message 2 of 8
southie
in reply to: mpeace

Maybe this?

(setq aa "1230C")

(substr aa 1 1)
"1"

(setq aa "A230C")

(substr aa 1 1)
"A"


HTH
Southie
Message 3 of 8
mpeace
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.
Message 4 of 8
MorganMcGuire
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.
Message 5 of 8
Anonymous
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.
Message 6 of 8
scot-65
in reply to: mpeace

(if (distof (substr 1 1) 2)
[yes]
[no]
);if
will also do the trick.
Just make sure
is a string.

Scot-65
A gift of extraordinary Common Sense does not require an Acronym Suffix to be added to my given name.


Message 7 of 8
mpeace
in reply to: mpeace

Thanks Jason, I was able to use your code and make it work for my needs.
Message 8 of 8
Anonymous
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"
--

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost