Jump to content

Remove the space from a text


Sweety

Recommended Posts

Wow wow wow . :shock: :)

 

Here what I got when I tested your routine with the Vlide and got the indication number 32 to the free space in the tested Text String .

 

(100 103 102 100 103 102 100 103 32 32 32 32 32 32 32 32 32 103 100 102 103 32 32 32 32 32 32 32 32 32 32 32 32 32 119 101 114 116 121)

 

Wonderful test of vl-string->list function .

 

Thanks for giving me the chance to do it myself.

 

Thanks and greatly appreciated Lee

 

Michaels

Link to comment
Share on other sites

  • Replies 62
  • Created
  • Last Reply

Top Posters In This Topic

  • Lee Mac

    15

  • alanjt

    14

  • Michaels

    8

  • Sweety

    8

Top Posters In This Topic

Posted Images

but do you understand exactly the numbers that are returned - i.e. how they relate to the characters?

 

I can say yes ,

 

Chech these numbers that are related to my name somehow and how the numbers are different than each other .

 

Look how did I write my name (First char in capital and the second one in small letter)

[color="red"]M[/color][color="#4169e1"]m[/color]ichael s

 

Here is the return of the vl-string->list function .

 

([color="red"]77[/color] [color="#4169e1"]109[/color] 105 99 104 97 101 108 [color="#ff00ff"]32[/color] 115)

 

So what I meant is that the capital and small letters are having different values of that return .

 

Nice try . :)

 

Thanks

Link to comment
Share on other sites

Wow thats really nice ,

 

I added one more vl-remove function to remove the capital letter in name and it did it well .

 

Mmichael s

 

(vla-put-TextString o (vl-list->string [color="red"](vl-remove 77[/color] (vl-remove 32 (vl-string->list (vla-get-TextString o))))))

 

And it become ...

 

michaels

 

So general question , could we build a text with these (ASCII printable characters) that are included within that list ?(strange question:unsure:)

 

Thanks Lee

Link to comment
Share on other sites

So general question , could we build a text with these (ASCII printable characters) that are included within that list ?(strange question:unsure:)

 

We are already... You have used the vl-list->string function in your code.

 

Also, look into the ascii and chr functions.

Link to comment
Share on other sites

I think it's a matter of turning the value of each char to become a char first , and after that we would use (vla-put-TextString ..... to replace or to add specific chars.

 

Am I right ?

Link to comment
Share on other sites

Here it goes . .. . . . .. .. :)

 

Try it with text entity not (mtext)

 

(defun c:underline (/ e )
 (vl-load-com)
 (while (setq e (car (entsel)))
       (if (vlax-property-available-p (setq o (vlax-ename->vla-object e)) 'TextString)
        (vla-put-TextString o (setq a (strcat (chr 37)(chr 37)(chr 117)(setq b (vla-get-TextString o)))))
    )
 (princ)
)

 

But what about your way with vl-list->string ?

 

I feel like dancing on the moon. :lol:

Michaels

Link to comment
Share on other sites

Yes, three different ways:

 

(defun c:test1 ( / e )
 (vl-load-com)
 (while (setq e (car (entsel)))
   (if (vlax-property-available-p (setq o (vlax-ename->vla-object e)) 'TextString)
     (vla-put-TextString o (strcat "%%u" (vla-get-TextString o)))
   )
 )
 (princ)
)

(defun c:test2 ( / e )
 (vl-load-com)
 (while (setq e (car (entsel)))
   (if (vlax-property-available-p (setq o (vlax-ename->vla-object e)) 'TextString)
     (vla-put-TextString o (vl-list->string (append '(37 37 117) (vl-string->list (vla-get-TextString o)))))
   )
 )
 (princ)
)

(defun c:test3 ( / e )
 (vl-load-com)
 (while (setq e (car (entsel)))
   (if (vlax-property-available-p (setq o (vlax-ename->vla-object e)) 'TextString)
     (vla-put-TextString o (vl-list->string (cons 37 (cons 37 (cons 117 (vl-string->list (vla-get-TextString o)))))))
   )
 )
 (princ)
)

For this task, the first is the most practical IMO

Link to comment
Share on other sites

Frankly , More than great Lee.

 

Have you noticed that you used my way instead of the one that you have used in your routine before ? :)

 

The way that I did it before .

(setq e (car ([color="red"]entsel[/color])))

The way that you did it before but not use it earlier .

(setq e (car ([color="red"]nentsel[/color])))

 

 

So any difference in the use of nentsel in our examples ?

Thank you so much Lee.

 

Greatly appreciated.

Link to comment
Share on other sites

Have you noticed that you used my way instead of the one that you have used in your routine before ?

 

Yes, I just modified your posted code :wink:

 

So any difference in the use of nentsel in our examples ?

 

Yes, nentsel allows for a selection of attributes, entsel does not.

 

Thank you so much Lee.

 

Greatly appreciated.

 

You're very welcome Michaels :)

Link to comment
Share on other sites

For this task, the first is the most practical IMO

Agreed. Unless you had a reason to already have the string converted to a list of ascii code, the others are pointless.

Link to comment
Share on other sites

  • 1 year later...

Is there a way to do this so that only "extra" spaces are removed? (I'm not a programmer). In other words, I don't want to remove spaces between words but only the spaces before and after each text string/sentence... or I suppose it could also be in a case where there are two spaces side-by-side and one of them needs to go away. :unsure: Thanks!

 

Compliments to Lee Mac for this code (I assume it's alright to repost this?)

 

 
(defun c:removespcs ( / e ) (vl-load-com)

(while (setq e (car (nentsel)))
(if (vlax-property-available-p (setq o (vlax-ename->vla-object e)) 'TextString)
(vla-put-TextString o (vl-list->string (vl-remove 32 (vl-string->list (vla-get-TextString o)))))
)
)

(princ)
)

Edited by ryoerger
To comply with forum rules.
Link to comment
Share on other sites

Welcome to Cad Tutor

 

I'm sure theres a more efficient way of doing this.

 

 
(defun c:removespcs ( / e str l p) (vl-load-com)
(while (setq e (car (nentsel)))
(if (and (vlax-property-available-p (setq o (vlax-ename->vla-object e)) 'TextString)
        (setq str (vla-get-TextString o)) (vl-string-search "  " str))
     (progn
 (while (setq p (vl-string-position 32 str nil t))
               (setq l (cons (strcat " " (substr str (+ 2 p))) l)
                              str (substr str 1 p)))
 (setq str (strcat str (apply 'strcat (vl-remove-if '(lambda (j)(eq j " ")) l)))
                     l nil)
               (vla-put-textstring o str)
               )
   )
     )

(princ)
)

 

What it does is search the string with "two spaces side-by-side " then rebuild the string.

 

BTW: its not limited to 2 spaces

Edited by pBe
Link to comment
Share on other sites

Welcome to Cad Tutor

 

What it does is search the string with "two spaces side-by-side " then rebuild the string.

 

BTW: its not limited to 2 spaces

 

Thank you!

 

...and, I have a feeling this often happens...

It work beautifully as designed, per my request, but now I realize I did not ask for what I really need. :oops:

In a text string like this (where the dashes represent spaces):

 

----SUMP-PUMP--1-CURRENT---MONITOR------

 

Your code created this:

 

-SUMP-PUMP-1-CURRENT-MONITOR-

 

Cool! But I really needed this:

 

SUMP-PUMP-1-CURRENT-MONITOR

 

Where there are no spaces on either end of the text string.

(something keeps telling me I need to become a programmer... :roll:)

Link to comment
Share on other sites

Are you sure the space at the end remains?

 

(defun c:removespcs ( / e str l p) (vl-load-com)
(while (setq e (car (nentsel)))
(if (and (vlax-property-available-p (setq o (vlax-ename->vla-object e)) 'TextString)
        (setq str (vla-get-TextString o)) (vl-string-search "  " str))
     (progn
 (while (setq p (vl-string-position 32 str nil t))
               (setq l (cons (strcat " " (substr str (+ 2 p))) l)
                              str (substr str 1 p)))
 (setq str [b][color=blue](vl-string-left-trim " "[/color][/b] (strcat str (apply 'strcat (vl-remove-if '(lambda (j)(eq j " ")) l))))
                     l nil)
               (vla-put-textstring o str)
               )
   )
     )

(princ)
)

 

EDIT: hang on.. i assumed every string would have a prefix at the front..

Edited by pBe
add vl-string-left trim
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...