Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

stripping multiple spaces i a string

11 REPLIES 11
Reply
Message 1 of 12
Anonymous
1445 Views, 11 Replies

stripping multiple spaces i a string

any function like vl-strig-trim ...
which will eliminate multiple spaces in a string, and keep only single ines
TIA
11 REPLIES 11
Message 2 of 12
Moshe-A
in reply to: Anonymous

Mark Hi,

try (vl-string-substr) function

Moshe
Message 3 of 12
Kent1Cooper
in reply to: Anonymous

This will do that:

{code}
(while (vl-string-search " " string)
(setq string (vl-string-subst " " " " string))
); end while
{code}

It looks for double spaces, and replaces them with single ones. Then, in case there might be *three* or more spaces in a row anywhere in the original, it goes back and looks again, and substitutes again if necessary, until it no longer finds any double ones.

--
Kent Cooper


Mark wrote...
any function like vl-strig-trim ...
which will eliminate multiple spaces in a string, and keep only single ines
Kent Cooper, AIA
Message 4 of 12
Kent1Cooper
in reply to: Anonymous

(vl-string-subst), actually....

--
Kent Cooper


moshe-a wrote...
try (vl-string-substr) function
Kent Cooper, AIA
Message 5 of 12
Anonymous
in reply to: Anonymous

excellent
i added (vl-string-trim " " string) to take care of trailing or leading
space...
thanks guys
m

"Mark" wrote in message
news:6389150@discussion.autodesk.com...
any function like vl-strig-trim ...
which will eliminate multiple spaces in a string, and keep only single ines
TIA
Message 6 of 12
Anonymous
in reply to: Kent1Cooper

Please can we have the whole lisp to delete double spaces including the (vl-string-trim " " string) for leading/trailing spaces?
Message 7 of 12
Kent1Cooper
in reply to: Anonymous


@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

Kent Cooper, AIA
Message 8 of 12
Lee_Mac
in reply to: Anonymous

sbanister wrote:
Please can we have the whole lisp to delete double spaces including the (vl-string-trim " " string) for leading/trailing spaces?

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

 

Message 9 of 12
Anonymous
in reply to: Kent1Cooper

When loading the lisp I get:

Command: ; error: bad argument type: numberp: nil


It loads though & works on leading & trailing spaces but not the double spaces


We are using AutoCAD 2014 if that makes any difference































.



IMPORTANT NOTICE
Confidentiality Notice: This email and any accompanying documents
contain confidential information intended for a specific individual
and purpose. The information is private and protected by law. If
you are not the intended recipient then please delete or return
this email and you are hereby notified that any disclosure, copying
or distribution, or the taking of any action based on the contents
of this information is strictly prohibited. (c) 2014 Hamworthy
Combustion Engineering Limited - All rights reserved
Message 10 of 12
Kent1Cooper
in reply to: Anonymous


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

Kent Cooper, AIA
Message 11 of 12
Anonymous
in reply to: Kent1Cooper

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
)































.



IMPORTANT NOTICE
Confidentiality Notice: This email and any accompanying documents
contain confidential information intended for a specific individual
and purpose. The information is private and protected by law. If
you are not the intended recipient then please delete or return
this email and you are hereby notified that any disclosure, copying
or distribution, or the taking of any action based on the contents
of this information is strictly prohibited. (c) 2014 Hamworthy
Combustion Engineering Limited - All rights reserved
Message 12 of 12
Kent1Cooper
in reply to: Anonymous


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

Kent Cooper, AIA

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost