Jump to content

Remove spaces from string


Jonathan Handojo

Recommended Posts

Hi all,

 

Does anyone have a function quite very similar to the trim command of a string that allows you to remove multiple spaces into just one.

 

For example, if you function is

(defun RemoveGaps (str / ;|your local variables|;)
    ; your function
    )

 

Then running (RemoveGaps "   This            is          a   normal         string.      ") will return "This is a normal string."

 

Thanks

Jonathan Handojo

 

Edited by Jonathan Handojo
Link to comment
Share on other sites

Just loop it look for 2 spaces replace with 1. Once wcmatch "  " not found do next string. Also need the "\n" and "\t" so removes spaces at start. 

 

From help

Substitutes one string for another, within a string

(vl-string-subst new-str pattern string [start-pos])

 

(setq str " Leave   me    alone ")

Command: (setq str (vl-string-subst " " "  " str 1))
" Leave  me    alone "
Command: (setq str (vl-string-subst " " "  " str 1))
" Leave me    alone "
Command: (setq str (vl-string-subst " " "  " str 1))
" Leave me   alone "
Command: (setq str (vl-string-subst " " "  " str 1))
" Leave me  alone "
Command: (setq str (vl-string-subst " " "  " str 1))
" Leave me alone "

Check if 1st character is " " and last is " "

"Leave me alone"
 

Edited by BIGAL
Link to comment
Share on other sites

Haha, how stupid of me. Thanks for that hint BIGAL.

 

I suppose for \n and \t, can always do (vl-string-translate "\n\t" "  " str) first before executing your above code.

Edited by Jonathan Handojo
Link to comment
Share on other sites

Here is one way without the use of converting string to list then list to string and without the use of lambda. ;) 

(defun weed:out:spaces ( s / n c g) (setq n "")
  ;; Tharwat - 14.Jul.2020	;;
  (while (/= "" (setq c (substr s 1 1)))
    (or (= g c " ") (setq n (strcat n c)))
    (setq g c s (substr s 2))
    )
  (vl-string-trim " " n)
  )

 

Edited by Tharwat
  • Thanks 1
Link to comment
Share on other sites

Thanks Tharwat. After hearing BIGAL's suggestion, I've come to deduce it to:

 

(defun RemoveGaps (str)
    (while (/= str (setq str (vl-string-subst " " "  " str))))
    (vl-string-trim " " str)
    )

 

Edited by Jonathan Handojo
'left & right trim' to just 'trim'
Link to comment
Share on other sites

You're welcome Jonathan.

Please note that you can replace the two trim functions left & right with one as I demonstrated earlier in my codes with vl-string-trim

Link to comment
Share on other sites

 

 

(setq $ (strcase (vl-princ-to-string (read "(   This            is          a   normal         string.      )")) t))
(substr $ 2 (- (strlen $) 2))

hiccups strcase

 

 

Link to comment
Share on other sites

(defun rh:rss (str) (while (vl-string-search "  " str) (setq str (vl-string-subst " " "  " str))) (vl-string-trim " " str))

 

Link to comment
Share on other sites

(defun _LostInSpace (str / p)
  (setq str (vl-string-trim " " str))
  ((lambda (s)
     (while (setq p (vl-string-position
		      32
		      str
		    )
	    )
       (setq s	 (strcat s (substr str 1 p) " ")
	     str (vl-string-trim " " (substr str (1+ p)))
       )
     )
     (strcat s str)
   )
    ""
  )
)

 

Link to comment
Share on other sites

1 hour ago, dlanorh said:

Danger! Will Robinson

 

 

That's the classic Robot. [ the nice one ]

 

But this is the baddie.. [ 1998 ]

image.png.e1f70d1e3ae601c8d8229d26953011e1.png

Destroy the Robinsons family!!! 

 

  • Like 1
Link to comment
Share on other sites

Another for fun :)

(defun _lostinspace2 (str / p)
  (apply 'strcat (mapcar '(lambda (s) (if (= 32 s) "" (chr s)))(vl-string->list str)))
)
(_lostinspace2 "this is a test of the radio  broadcast     system")
;;"thisisatestoftheradiobroadcastsystem" 

Oops .. read the title too literally. 🥴 .. this just removes all spaces.

Edited by ronjonp
*code does not meet the requirement
Link to comment
Share on other sites

 

 

(defun foo (x l)
  (if l
    (vl-remove nil (cons (if (not (= x (car l) (cadr l))) (car l)) (foo x (cdr l))))
  )
)

(defun ?_? (str)
  (vl-string-trim " "
                  (vl-list->string
                    (foo 32 (vl-string->list str))
                  )
  )
)

 

 

4 hours ago, pBe said:

_LostInSpace 

 

:sweat: Mayday oxygen

 

 

Link to comment
Share on other sites

Another one

(defun disspacify (str / regexp)
  (if
    (setq regexp (vlax-get-or-create-object "vbscript.regexp"))
    (progn
      (vlax-put-property regexp 'global actrue)
      (vlax-put-property regexp 'pattern " +")
      (vlax-invoke regexp 'replace (vl-string-trim " " str) " ")
    )
  )
)
_$ (disspacify "   This            is          a   normal         string.      ")
"This is a normal string."
_$ 

[Edit]: Why not full regular expression... Plus, the dot or comma inside the string needs special treatment:

(defun disspacify (str / regexp)
  (if
    (setq regexp (vlax-get-or-create-object "vbscript.regexp"))
    (progn
      (vlax-put-property regexp 'global actrue)
      (foreach x '(
                    (" +[.]|[.]" . ". ");"end ."  to "end. "
                    (" +[,]|[,]" . ", ");"mid ,"  to "mid, "
                    (" +"        .  " ");replace multiple spaces
                    ("^ +| +$"   .   "");remove start and end space(s)
                  )
        (vlax-put-property regexp 'pattern (car x))
        (setq str (vlax-invoke regexp 'replace str (cdr x)))
      )  
    )
  )
)
_$ (disspacify "   This   is   a double     sentence ,the   other   one    is     not    .This   is   a   normal      string  .      ")
"This is a double sentence, the other one is not. This is a normal string."
_$ 

 

Edited by Stefan BMR
  • Like 3
Link to comment
Share on other sites

2 hours ago, Stefan BMR said:

Another one


(defun disspacify (str / regexp)
  (if
    (setq regexp (vlax-get-or-create-object "vbscript.regexp"))
    (progn
      (vlax-put-property regexp 'global actrue)
      (vlax-put-property regexp 'pattern " +")
      (vlax-invoke regexp 'replace (vl-string-trim " " str) " ")
    )
  )
)

_$ (disspacify "   This            is          a   normal         string.      ")
"This is a normal string."
_$ 

[Edit]: Why not full regular expression... Plus, the dot or comma inside the string needs special treatment:


(defun disspacify (str / regexp)
  (if
    (setq regexp (vlax-get-or-create-object "vbscript.regexp"))
    (progn
      (vlax-put-property regexp 'global actrue)
      (foreach x '(
                    (" +[.]|[.]" . ". ");"end ."  to "end. "
                    (" +[,]|[,]" . ", ");"mid ,"  to "mid, "
                    (" +"        .  " ");replace multiple spaces
                    ("^ +| +$"   .   "");remove start and end space(s)
                  )
        (vlax-put-property regexp 'pattern (car x))
        (setq str (vlax-invoke regexp 'replace str (cdr x)))
      )  
    )
  )
)

_$ (disspacify "   This   is   a double     sentence ,the   other   one    is     not    .This   is   a   normal      string  .      ")
"This is a double sentence, the other one is not. This is a normal string."
_$ 

 

 

Well, I'm only needing it for one sentence, and one consisting of letters. Otherwise you've got a lot more punctuations to consider other than just dots and commas...

 

" I      spent   $   2  .  00      on      ice    -    cream .  "

Link to comment
Share on other sites

15 minutes ago, Jonathan Handojo said:

 

Well, I'm only needing it for one sentence, and one consisting of letters. Otherwise you've got a lot more punctuations to consider other than just dots and commas...

 

" I      spent   $   2  .  00      on      ice    -    cream .  "

 

You're right, I opened Pandora's box and now I can't handle it, sorry for that.

  • Funny 1
Link to comment
Share on other sites

3 hours ago, Stefan BMR said:

 

You're right, I opened Pandora's box and now I can't handle it, sorry for that.

 

Maybe someone will be able to close it. I don't need it to be closed though, the solutions offered here are good enough for me.

Link to comment
Share on other sites

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