Visual LISP, AutoLISP and General Customization
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
Test String for First Character
Options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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.
Re: Test String for First Character
Options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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
(setq aa "1230C")
(substr aa 1 1)
"1"
(setq aa "A230C")
(substr aa 1 1)
"A"
HTH
Southie
Re: Test String for First Character
Options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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.
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.
Re: Test String for First Character
Options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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.
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
Re: Test String for First Character
Options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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.
[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]
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.
Re: Test String for First Character
Options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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.
[yes]
[no]
);if
will also do the trick.
Just make sure is a string.
Re: Test String for First Character
Options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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
Re: Test String for First Character
Options
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
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"
--
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"
--
