Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

Reply
Valued Contributor
mpeace
Posts: 95
Registered: ‎01-24-2006
Message 1 of 8 (73 Views)

Test String for First Character

73 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.
Mentor
southie
Posts: 291
Registered: ‎03-15-2007
Message 2 of 8 (73 Views)

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
Valued Contributor
mpeace
Posts: 95
Registered: ‎01-24-2006
Message 3 of 8 (73 Views)

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.
Distinguished Contributor
MorganMcGuire
Posts: 111
Registered: ‎07-05-2007
Message 4 of 8 (73 Views)

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.
*Jason Piercey
Message 5 of 8 (73 Views)

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.
*Expert Elite*
scot-65
Posts: 2,189
Registered: ‎12-11-2003
Message 6 of 8 (73 Views)

Re: Test String for First Character

03-27-2009 02:44 PM 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
Dyslexia is a permanent physical disability that cannot be seen.
Introverts is proof that there is indeed intelligent life on this planet.
Valued Contributor
mpeace
Posts: 95
Registered: ‎01-24-2006
Message 7 of 8 (73 Views)

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.
*Marc'Antonio Alessi
Message 8 of 8 (73 Views)

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"
--
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.