Jump to content

Recommended Posts

Posted

Dear friends,

 

I want to remove the spaces in a particular string by the following code.But it is not returning my requirement.

 

(strcase (vl-string-translate " " "" "8 x 150 x 2974"))

returns "8 X 150 X 2974"

 

 

(strcase (vl-string-translate " " "a" "8 x 150 x 2974"))

returns

"8AXA150AXA2974"

 

Pls help me in this. Do you know any other than this function?

 

Yours,

Muthu.

Posted

muthu123,

 

Try this, it is quick and dirty, but it works.

 

 
(defun rem-spc (str / )
 (setq str-list (vl-string->list str))
 (setq new-list '("NIL"))
 (foreach x str-list
   (if (/= x 32)
     (setq new-list (append new-list (list x )))))
 (setq new-list (cdr new-list))
 (setq new-str-list (vl-list->string new-list))
 )

Syntax:

 
(setq str (rem-spc "This String Contains Spaces."))

Returns: "ThisStringContainsSpaces."

 

The function will remove spaces in the string. Though you must note that there isn't any error checking in the function. But feel free to modify as needed.

 

Regards,

Hippe013

Posted

Hippe,

 

Why the "NIL"?

 

(defun nospace ( s )
 (vl-list->string (vl-remove 32 (vl-string->list s)))
)

Posted

Lee

 

After the fact, I had noticed that this one was already solved. Yup, my bad.

 

As for why the nil... Well... No Reason Now!:oops:

 

I didn't quite understand the append. I thought it wasn't able to append to a nil symbol. So as a work around I would create a list with a dummy first member '("NULL"). At the end of the process I would then just remove the first item of the list. '("NULL" 0 1 2 3 4) -> '(0 1 2 3 4) But after some looking into it I find that: ... You can!

 

 
(setq li nil)
(setq cnt 0)
(repeat 5
 (setq li (append li (list cnt)))
 (setq cnt (1+ cnt))
 li
 )

 

Thanks :wink:

Posted

'cons' is the best and fastest solution when creating lists - only downside is that the list will be in reverse.

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...