Jump to content

Remove Only Alphabetical Characters from String


Recommended Posts

Posted

Is there an easy way to do this? I am trying to use vl-string-left-trim to remove all characters that are either a letter or a symbol. I am able to remove them as long as I know everything that will ever be in this string, but I don't know if I always will. I would rather have a safety net that would catch anything just in case it changes.

  • Replies 26
  • Created
  • Last Reply

Top Posters In This Topic

  • ksperopoulos

    9

  • Tharwat

    8

  • Lee Mac

    5

  • pBe

    2

Top Posters In This Topic

Posted

Use vl-string->list to convert the string to a list of ASCII character codes, and then use vl-remove-if with an appropriate predicate function to process this list, removing those codes which do not meet your criteria, before using vl-list->string to convert the resulting list of ASCII codes back to a string.

 

Here is an ASCII character code reference.

Posted

Lee what about your parse numbers.lsp but with ascii values set to be non numbers as an example of method.

Posted

Lee Mac - thank you for the response. I tried a similar method to what you are suggesting earlier today with no success. I got the ascii codes returned after running vl-string->list but when I try to use vl-remove-if, I get an error stating "bad argument type for compare". I know it is probably somehow linked to a mistake I am making trying to get the value to return "T" but my brain is fried. I think my searches are leading me down the right path on how to do things, but my inexperience keeps me from moving very quickly in that direction. I will pick this back up tomorrow.

Posted (edited)

An example ...

 

(vl-list->string (vl-remove-if (function (lambda (u) (or (< 64 u 91) (< 96 u 123)))) (vl-string->list "Abcd12345XwZ")))

Edited by Tharwat
Thanks Marko
Posted

Look into highlighted number, Tharwat...

 

(vl-list->string (vl-remove-if (function (lambda (u) (or (< 64 u 91) (< [highlight]96[/highlight] u 123)))) (vl-string->list "Abcd12345XwZ")))

Posted
Look into highlighted number, Tharwat...

 

Thanks Marko for the correction , Appreciated a lot .

Posted
An example ...

Why not?

 

(vl-list->string
     (vl-remove-if-[b]not[/b]
(function (lambda (u) [b](< 47 u 58)[/b]))
(vl-string->list str)
     )
   )

Posted

Vanilla

 

(defun numbers1 (str / a b)
 (setq a "")
 (repeat (strlen str)
   (if	(< 47 (ascii (setq b (substr str 1 1))) 58)
     (setq a (strcat a b))
   )
   (setq str (substr str 2))
 )
 a
)

 

(defun numbers2 (str / a b)
 (repeat (strlen str)
   (if	(< 47 (ascii (setq b (substr str 1 1))) 58)
     (setq a (cons b a))
   )
   (setq str (substr str 2))
 )
 (apply 'strcat (reverse a))
)

Posted
Why not?

 

Another ... 8)

 

(mapcar 'vl-list->string
       (list (vl-remove-if
               '(lambda (u)
                  (not (member u '(48 49 50 51 52 53 54 55 56 57))
                  )
                )
               (vl-string->list "Abcd12345X7789wZ")
             )
       )
)

Posted
Lee what about your parse numbers.lsp but with ascii values set to be non numbers as an example of method.

 

That certainly provides another route for the OP to consider.

 

Though, I'm not entirely sure of the desired result, i.e. whether the OP wishes to retain only integers or also retain hyphens & periods and hence decimal numbers also.

 

Vanilla

 

Nice one pBe :thumbsup:

 

Here is a recursive alternative:

(defun numbers ( s / c )
   (cond
       (   (wcmatch s "~*#*") "")
       (   (zerop (setq c (ascii s))) s)
       (   (< 47 c 58) (strcat (chr c) (numbers (substr s 2))))
       (   (numbers (substr s 2)))
   )
)

Another ... 8)

(mapcar 'vl-list->string
       (list (vl-remove-if
               '(lambda (u)
                  (not (member u '(48 49 50 51 52 53 54 55 56 57))
                  )
                )
               (vl-string->list "Abcd12345X7789wZ")
             )
       )
)

 

I don't understand why you are using mapcar here?

It just seems redundant as you seem to be constructing a list for the sole purpose of using mapcar in the code, which will then return a list rather than a string...

Posted

I don't understand why you are using mapcar here?

 

Because the return of the (list (vl-remove-if ..... would be a list , and I know it is a little bit long one so we can shorten it to this without brackets all around as the previous one of course .

 

(vl-list->string
 (vl-remove-if
   '(lambda (u)
      (not (member u '(48 49 50 51 52 53 54 55 56 57))
      )
    )
   (vl-string->list "Abcd12345X7789wZ")
 )
)

Posted
Because the return of the (list (vl-remove-if ..... would be a list

 

Exactly my point - the code is constructing an unnecessary list, and then processing this single-item list using mapcar - both the list and mapcar are entirely redundant as you have shown with the code in your next post.

Posted

The way Tharwat shows with all the ascii codes listed is the path I started down, but when I got to lambda, my brain couldn't fit anything else in for the day. I drank a couple beers, lost a few brain cells, and am back at it again today. Hopefully it will stick. Thanks for all the suggestions!

 

BTW - I am looking at keeping all numbers and a period.

Posted

(vl-list->string
 (vl-remove-if
   '(lambda (u)
      (not (member u '([color=blue]32[/color] 48 49 50 51 52 53 54 55 56 57))
      )
    )
   (vl-string->list "Abcd12345X7789wZ")
 )
)

 

 

BTW - I am looking at keeping all numbers and a period.

 

Just by adding the number 32 would take care of the period factor :D

Posted
Just by adding the number 32 would take care of the period factor :D

 

Wouldn't the ascii code be 46, not 32?

Posted
Wouldn't the ascii code be 46, not 32?

If you are talking about the decimal point , then you are right but if it is a free space it should be 32 .

Posted

Sorry, decimal point (period) is what I meant. Thanks for clarifying.

Posted
Sorry, decimal point (period) is what I meant. Thanks for clarifying.

 

You're welcome .

Posted

Does lambda need more than one argument. I keep getting an error that says there are too few arguments?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...