@Anonymous wrote:
Please can we have the whole lisp to delete double spaces including the (vl-string-trim " " string) for leading/trailing spaces?
This will do that. It has not only the -trim part added, but it also needed the quoted double spaces in the original restored [something about a system switchover since then must have changed them to single spaces]:
(while (vl-string-search " " string); any two [or more] spaces together anywhere?
(setq string (vl-string-subst " " " " string)); change two to one
); end while
(setq string (vl-string-trim " " string)); remove any spaces from both ends
Here is a draft of a complete program:
(defun c:trimspace ( / e i s x ) (if (setq s (ssget "_:L" '((0 . "TEXT") (1 . "* *")))) (repeat (setq i (sslength s)) (setq e (entget (ssname s (setq i (1- i)))) x (vl-string-trim " " (cdr (assoc 1 e))) ) (while (/= x (setq x (vl-string-subst " " " " x)))) (entmod (list (assoc -1 e) (cons 1 x))) ) ) (princ) )
@Anonymous wrote:
When loading the lisp I get:
Command: ; error: bad argument type: numberp: nil
....
Since that's in reply to me, if you are loading my code, rather than Lee_Mac's, there's nothing in it that's asking for a numerical value that could be getting a nil value instead, so I'd want to see how you wrapped it in something to make it into a loadable function definition. Also, be sure you're using my later one, not the earlier one that's been altered somehow in the passage of time in a way that would explain its not taking care of double spaces.
@Anonymous wrote:
defun c:BB ( / string)
(while (vl-string-search " " string); any two [or more] spaces together anywhere?
(setq string (vl-string-subst " " " " string)); change two to one
); end while
(setq string (vl-string-trim " " string)); remove any spaces from both ends
)
....
[By the way, is that huge amount of white space a characteristic of your Signature definition? Please pare it down. And make a separate NOTICE for these Forums, if any is needed at all -- that one's about e-mail, and seems rather strangely worded for something posted in a public Forum.]
[I assume you really have an opening left parenthesis before the word defun....]
You would need to either make it a function with the string as an argument, or a command in which the User supplies the string, but your code has elements of both approaches.
As a function with an argument:
(defun BB (string) ; without C:, no need for / , but if you use it, string must be before it
(while (vl-string-search " " string); any two [or more] spaces together anywhere?
(setq string (vl-string-subst " " " " string)); change two to one
); end while
(setq string (vl-string-trim " " string)); remove any spaces from both ends
)
Usage:
(BB " Your text string to be fixed ")
including the parentheses. The string can be supplied in a variable rather than spelled out:
(BB yourvariablename)
As a command:
(defun C:BB () ; with C:, string not mentioned here [but see below]
(setq string (getstring "\nString to be fixed: "))
(while (vl-string-search " " string); any two [or more] spaces together anywhere?
(setq string (vl-string-subst " " " " string)); change two to one
); end while
(setq string (vl-string-trim " " string)); remove any spaces from both ends
)
[It could be made to ask the User to select a Text object and extract its text content, with or without correcting it in the Text object, if desired.]
That will return the corrected string, and it will remain in the string variable for use by something else. If you want it only returned and not retained in that variable, start it with:
(defun C:BB (/ string); string as localized variable
NOTE that something in "the system" seems to be conspiring to spoil this, by turning double spaces into single ones. Make sure that everywhere I have red double quotes " ", there are two spaces between them in your code, even if there aren't as they turn out here. [EDIT: It survived my posting with the double spaces intact, so I assume it must be something in your end that pared them down.]