Author Topic: Know if a string is letter or number  (Read 8857 times)

0 Members and 1 Guest are viewing this topic.

the7347

  • Guest
Know if a string is letter or number
« on: April 20, 2015, 09:52:57 AM »
Hello community

As I can know if a string is letter or number, example "A" is a letter, "7" a number.

simple but I know of no function

thanks...

ronjonp

  • Needs a day job
  • Posts: 7529
Re: Know if a string is letter or number
« Reply #1 on: April 20, 2015, 10:11:24 AM »
Code - Auto/Visual Lisp: [Select]
  1. (numberp (read "7"))

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Know if a string is letter or number
« Reply #2 on: April 20, 2015, 10:24:49 AM »
A few ways that spring to mind:

The following will return T if the content is numerical:
Code: [Select]
(numberp (read <str>))
Code: [Select]
(member (type (read <str>)) '(int real))
Code: [Select]
(distof <str> 2)
Code: [Select]
(wcmatch <str> "~*[~0-9]*")
Code: [Select]
(vl-every '(lambda ( x ) (< 47 x 58)) (vl-string->list <str>))
The last two are for integers only.
« Last Edit: April 20, 2015, 11:11:38 AM by Lee Mac »

GP

  • Newt
  • Posts: 83
  • Vercelli, Italy
Re: Know if a string is letter or number
« Reply #3 on: April 20, 2015, 10:56:28 AM »
Even (maybe):

Code: [Select]
(eval (read <str>))

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: Know if a string is letter or number
« Reply #4 on: April 20, 2015, 11:48:04 AM »
Code: [Select]
Comando: (setq astring ".2")
".2"

Comando: (numberp (read astring))
; errore: punto in posizione non corretta nell'input

Comando: (member (type (read astring)) '(int real))
; errore: punto in posizione non corretta nell'input

Comando: (distof astring 2)
0.2

Comando: (wcmatch astring "~*[~0-9]*")
nil

Comando: (vl-every '(lambda ( x ) (< 47 x 58)) (vl-string->list astring))
nil


Comando: (setq astring "0.2")
"0.2"

Comando: (numberp (read astring))
T

Comando: (member (type (read astring)) '(int real))
(REAL)

Comando: (distof astring 2)
0.2

Comando: (wcmatch astring "~*[~0-9]*")
nil

Comando: (vl-every '(lambda ( x ) (< 47 x 58)) (vl-string->list astring))
nil

I think this is better:
Code: [Select]
(defun IsnumericP (str)
  (numberp (distof str))
)

Comando: (IsnumericP "2")
T
Comando: (IsnumericP "2.0")
T
Comando: (IsnumericP ".2")
T

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Know if a string is letter or number
« Reply #5 on: April 20, 2015, 12:35:20 PM »
Since any non-nil value will validate a conditional function, the expression need not explicitly return T.

(I did note that my last two examples were compatible with integers only - positive integers, I would add)

Lee

the7347

  • Guest
Re: Know if a string is letter or number
« Reply #6 on: April 22, 2015, 10:47:14 AM »
wow .... I'm amazed at the number of responses

I am grateful

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: Know if a string is letter or number
« Reply #7 on: April 22, 2015, 12:02:20 PM »
Since any non-nil value will validate a conditional function, the expression need not explicitly return T.
...
Sorry, I do not understand what you mean, maybe the more correct name of the function  was IsNumeric or NumericP?

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Know if a string is letter or number
« Reply #8 on: April 22, 2015, 05:08:59 PM »
Since any non-nil value will validate a conditional function, the expression need not explicitly return T.
...
Sorry, I do not understand what you mean, maybe the more correct name of the function  was IsNumeric or NumericP?

For example, if an operation depends upon the content of a given string being numerical, the test for numerical content need not return a strict boolean value, since any non-nil value will validate a conditional function.

Therefore, rather than:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:test ( / str )
  2.     (setq str (getstring "\nEnter some content: "))
  3.     (if (numberp (distof str 2))
  4.         (princ "\nYou entered a number.")
  5.         (princ "\nYou didn't enter a number.")
  6.     )
  7.     (princ)
  8. )

The code can be:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:test ( / str )
  2.     (setq str (getstring "\nEnter some content: "))
  3.     (if (distof str 2)
  4.         (princ "\nYou entered a number.")
  5.         (princ "\nYou didn't enter a number.")
  6.     )
  7.     (princ)
  8. )

Since distof will return nil if the string content is not numerical.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: Know if a string is letter or number
« Reply #9 on: April 23, 2015, 04:03:55 AM »
...
Since distof will return nil if the string content is not numerical.
Grazie mille for the explanation, all okay...     ...but there is at least one exception (that I know):
Code: [Select]
Comando: (distof "1/1")
1.0
Comando: (numberp "1/1")
nil

David Bethel

  • Swamp Rat
  • Posts: 656
Re: Know if a string is letter or number
« Reply #10 on: April 23, 2015, 07:39:00 AM »
for a single character, maybe :
Code: [Select]
(setq str "7")
(wcmatch str "#")
R12 Dos - A2K

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Know if a string is letter or number
« Reply #11 on: April 23, 2015, 09:15:41 AM »
...
Since distof will return nil if the string content is not numerical.
Grazie mille for the explanation, all okay...     ...but there is at least one exception (that I know):
Code: [Select]
Comando: (distof "1/1")
1.0
Comando: (numberp "1/1")
nil
I think you are confused here. The numberp function will return nil for any string.
Note that both of Lee's last examples retain a call to the distof function.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: Know if a string is letter or number
« Reply #12 on: April 23, 2015, 09:30:28 AM »
...
Since distof will return nil if the string content is not numerical.
Grazie mille for the explanation, all okay...     ...but there is at least one exception (that I know):
Code: [Select]
Comando: (distof "1/1")
1.0
Comando: (numberp "1/1")
nil
I think you are confused here. The numberp function will return nil for any string.
Note that both of Lee's last examples retain a call to the distof function.
Sorry, I had to write:
Code: [Select]
Comando: (numberp 1/1)
nil
Question: if  (distof "1/1") = 1.0   1/1 is a number?

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: Know if a string is letter or number
« Reply #13 on: April 23, 2015, 09:48:45 AM »
Maybe it is better to write:
(distof "17 1/2")  = > 17.5

so 17 1/2 it is not a number but the result of distof 17.5 is a number.      :?   ?

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: Know if a string is letter or number
« Reply #14 on: April 23, 2015, 10:30:07 AM »
Is this a valid test?
Code: [Select]
; Rel. 1.1
(defun NumericP (str)
  (and
    (distof str)
    (numberp (read (strcat "0" str)))
  )
)
(NumericP "1/1a") => nil
(NumericP "1/1" ) => nil
(NumericP "1.0" ) => T
(NumericP  ".1" ) => T