Jonathan Handojo Posted July 14, 2020 Posted July 14, 2020 (edited) 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 July 14, 2020 by Jonathan Handojo Quote
BIGAL Posted July 14, 2020 Posted July 14, 2020 (edited) 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 July 14, 2020 by BIGAL Quote
Jonathan Handojo Posted July 14, 2020 Author Posted July 14, 2020 (edited) 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 July 14, 2020 by Jonathan Handojo Quote
BIGAL Posted July 14, 2020 Posted July 14, 2020 No worries I am sure someone will do a smart lambda function. Quote
Tharwat Posted July 14, 2020 Posted July 14, 2020 (edited) 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 July 14, 2020 by Tharwat 1 Quote
Jonathan Handojo Posted July 14, 2020 Author Posted July 14, 2020 (edited) 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 July 14, 2020 by Jonathan Handojo 'left & right trim' to just 'trim' Quote
Tharwat Posted July 14, 2020 Posted July 14, 2020 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 Quote
Jonathan Handojo Posted July 14, 2020 Author Posted July 14, 2020 Ohh, right. Nice. Modified that one Quote
hanhphuc Posted July 14, 2020 Posted July 14, 2020 (setq $ (strcase (vl-princ-to-string (read "( This is a normal string. )")) t)) (substr $ 2 (- (strlen $) 2)) hiccups strcase Quote
dlanorh Posted July 14, 2020 Posted July 14, 2020 (defun rh:rss (str) (while (vl-string-search " " str) (setq str (vl-string-subst " " " " str))) (vl-string-trim " " str)) Quote
pBe Posted July 14, 2020 Posted July 14, 2020 (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) ) "" ) ) Quote
dlanorh Posted July 14, 2020 Posted July 14, 2020 1 hour ago, pBe said: (defun _LostInSpace (str / p) Danger! Will Robinson Quote
pBe Posted July 14, 2020 Posted July 14, 2020 1 hour ago, dlanorh said: Danger! Will Robinson That's the classic Robot. [ the nice one ] But this is the baddie.. [ 1998 ] Destroy the Robinsons family!!! 1 Quote
ronjonp Posted July 14, 2020 Posted July 14, 2020 (edited) 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 July 15, 2020 by ronjonp *code does not meet the requirement Quote
hanhphuc Posted July 14, 2020 Posted July 14, 2020 (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 Mayday oxygen Quote
Stefan BMR Posted July 14, 2020 Posted July 14, 2020 (edited) 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 July 14, 2020 by Stefan BMR 3 Quote
BIGAL Posted July 14, 2020 Posted July 14, 2020 Who thought a simple find replace request could end up with so many different versions. Quote
Jonathan Handojo Posted July 15, 2020 Author Posted July 15, 2020 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 . " Quote
Stefan BMR Posted July 15, 2020 Posted July 15, 2020 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. 1 Quote
Jonathan Handojo Posted July 15, 2020 Author Posted July 15, 2020 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. Quote
Recommended Posts
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.