Jump to content

Remove last 5 characters from a string


waders

Recommended Posts

I'm looking for some code to remove the last 5 letters from some text.

Example: New OR Equipment Boom Mb02.

I want to remove the space plus Mb02. So I have "New OR Equipment Boom".

Thanks

Link to comment
Share on other sites

Please note that a full stop & a space both counts as characters of a string, so you may need to be careful how many characters you remove

 

This is what I use:

(defun c:Char-Rem-R ( / ent idx nol ss tmp)
   (command "UNDO" "BE")
 (if (setq ss (ssget "_:L" '((0 . "MTEXT,TEXT"))))
       (progn
          (setq nol (getint "\n Number of Characters to Remove from Front of String <1>:"))
   	   (if (eq nol nil) (setq nol 1))
		(repeat (setq idx (sslength ss))
			(setq ent (entget(ssname ss (setq idx (1- idx)))))
			  (if (> (strlen (cdr(assoc 1 ent))) nol)
			    (progn
			(setq ent (subst (cons 1 (substr (cdr(assoc 1 ent)) 1 (-(strlen (cdr(assoc 1 ent))) nol ))) (assoc 1 ent) ent) )	  
			(entmod ent)
			    )
		            (setq tmp 1)
		  	    )  		    
		 )
	)
 (princ "No Text Selected. Please Try Again")
 )
 (command "UNDO" "END")
 (if (= tmp 1)
   (princ "\nSome Selected Text Have Less Characters than Number of Characters to Remove! (No Characters removed from these)")
 )
(princ)
)       

 

It defaults to 1 character, but it can easily be set to 5 by changing the following lines from:

(setq nol (getint "\n Number of Characters to Remove from End of String <1>:"))
	(if(= nol nil) (setq nol 1))  

 

to:

 

(setq nol (getint "\n Number of Characters to Remove from End of String <5>:"))
	(if(= nol nil) (setq nol 5))  

 

I also use a Char-Rem-L to remove characters from the beginning of a string if wanted?

 

- Harry H

Edited by Happy Hobbit
Code amended 22/03/2016
Link to comment
Share on other sites

Must be sure if the string length is bigger than the number of chars to be cut off.

 

(setq st "New OR Equipment Boom Mb02")

(if (< 5 (strlen st))
 (substr st 1 (- (strlen st) 5))
 )

Link to comment
Share on other sites

Harry , if you'd like to know my opinion and won't ignore it just consider the following for your information.

 

* There is no need to have two if functions , so just move the getint function down and add and function to obtain them like this:

 (if (and (setq ss (ssget "_:L" '((0 . "MTEXT,TEXT"))))
          (setq nol (getint "\n Number of Characters to Remove from End of String <1>:"))           
          )
    (.........

 

* Reduce the quantity of codes and variables and what's more check if the chars in the string is much in quantity than user wants:

 

(setq st (cdr (assoc 1 ent)))

(if (< nol (strlen st))
 (entmod (subst (cons 1 (substr st 1 (- (strlen st) nol)))
                (assoc 1 ent)
                ent
         )
 )
)

 

Good luck.

Link to comment
Share on other sites

Tharwat,, I have already amended my lisp to reflect your point about testing the number of characters in the selected string (s) compared with the number to be removed. I take your point about using an AND & shall incorporate that too. Thank you Tharwat.

 

Amended code in post #2

Link to comment
Share on other sites

You are welcome Harry.

 

Just one more thing if you are after giving the user to hit enter to accept the number 1 as default, have a look at the following simple codes;

 

(if (and (setq nol (cond ((getint "\nNumber of Characters to Remove from End of String <1>:"))
                        (t 1)
                        )
              )
        (princ (strcat "\nSelect (M)Texts to cut off last <" (itoa nol) "> chars "))
        (setq ss (ssget "_:L" '((0 . "MTEXT,TEXT"))))                    
          )
    (....

Link to comment
Share on other sites

Just one more thing if you are after giving the user to hit enter to accept the number 1 as default, have a look at the following simple codes;

(if (and (setq nol (cond ((getint "\nNumber of Characters to Remove from End of String <1>:"))
                        (t 1)
                        )
              )

 

Note that there is no need for the if/and in this case, as the cond expression will never return nil, regardless of the user input.

Link to comment
Share on other sites

Note that there is no need for the if/and in this case, as the cond expression will never return nil, regardless of the user input.

 

Yeah agree and that's why I changed the position of the getint function from the first example that I posted above.

 

Harry, your codes will not work if the user hit enter to accept the default value that you included in the message text with getint function which is

Link to comment
Share on other sites

Yeah agree and that's why I changed the position of the getint function from the first example that I posted above.

 

My point was that you needn't test the result of the cond expression to validate the test expression for the if statement, as the cond expression will never return nil.

Link to comment
Share on other sites

Frankly I did not like the idea to move the cond expression out of and evaluation since that I should check if the 'nol' variable is not equal to nil

Link to comment
Share on other sites

Or you could do a lot of error checking :

 

 

[b][color=BLACK]([/color][/b]defun rtrim [b][color=FUCHSIA]([/color][/b]s n / tmp[b][color=FUCHSIA])[/color][/b]
[b][color=FUCHSIA]([/color][/b]and [b][color=NAVY]([/color][/b]= [b][color=MAROON]([/color][/b]type s[b][color=MAROON])[/color][/b] 'STR[b][color=NAVY])[/color][/b]
     [b][color=NAVY]([/color][/b]> [b][color=MAROON]([/color][/b]strlen s[b][color=MAROON])[/color][/b] n[b][color=NAVY])[/color][/b]
     [b][color=NAVY]([/color][/b]= [b][color=MAROON]([/color][/b]type n[b][color=MAROON])[/color][/b] 'INT[b][color=NAVY])[/color][/b]
     [b][color=NAVY]([/color][/b]not [b][color=MAROON]([/color][/b]minusp n[b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
     [b][color=NAVY]([/color][/b]setq tmp [b][color=MAROON]([/color][/b]substr s 1 [b][color=GREEN]([/color][/b]- [b][color=BLUE]([/color][/b]strlen s[b][color=BLUE])[/color][/b] n[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]
tmp[b][color=BLACK])[/color][/b]

[b][color=BLACK]([/color][/b]prin1 [b][color=FUCHSIA]([/color][/b]rtrim [color=#2f4f4f]"12345678"[/color] 5[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]

 

-David

Link to comment
Share on other sites

Thanks to you all for the reply's. I got it to work. I guess I could have been a little clear in my request. But the end result was the same. Here is that part of the code. This site is awesome :D

(defun c:test (/) ;this is only here to show it works
(setq
 $path (vl-string-right-trim "\\" (getvar 'dwgprefix))
 $path (substr path 1 (vl-string-position (ascii "\\") $path 0 T))
 $Folder (substr $path (+ 2 (vl-string-position (ascii "\\") $path 0 T)))
)
(setq $ProjectNo (strcat (substr $Folder 1 5)))
(setq $String (strcat (substr $Folder 7)))
(setq $Title (if (< 5 (strlen $String)) (substr $String 1 (- (strlen $String) 5))))
(princ) ;this is only here to show it works
) ;this is only here to show it works

Link to comment
Share on other sites

Frankly I did not like the idea to move the cond expression out of and evaluation since that I should check if the 'nol' variable is not equal to nil

 

That's the point: the 'nol' variable can never be nil, as the cond expression cannot return nil.

Link to comment
Share on other sites

That's the point: the 'nol' variable can never be nil, as the cond expression cannot return nil.

As you might have seen in my previous posts in this thread I wanted to keep the user inputs in one if expression to avoid extra work and that was my way of building the codes and to keep it simple to Harry although I see he got confused and did not like the way I commented the codes or so.

 

I'm keeping outta this, my lisp works & I don't care

 

Harry, I did not mean to disappoint you and all of these comments were written for you to learn and keep coding forward and no offence meant at all and that is why in my first reply to you I said "if you'd like and won't ignore it" , so it is up to you if you don't like to hear from me any more. sorry if you felt that I am not encouraging you.

  • Like 1
Link to comment
Share on other sites

Not at all Tharwat, I'm very grateful for your time & points raised about my code. I actually incorporated your first two suggestions into the lisp, see post #2. I just wanted to keep out of a slanging match between you & Lee

Link to comment
Share on other sites

I just wanted to keep out of a slanging match between you & Lee

 

Excuse me?

 

Maybe you are misinterpreting the content of the thread, as I am merely remarking on the difference between this:

(if
   (and
       (setq nol (cond ((getint "\nNumber of Characters to Remove from End of String <1>:")) (t 1)))
       (setq ss  (ssget "_:L" '((0 . "MTEXT,TEXT"))))
       ...
   )
)

And this:

(setq nol (cond ((getint "\nNumber of Characters to Remove from End of String <1>:")) (t 1)))
(if (setq ss (ssget "_:L" '((0 . "MTEXT,TEXT"))))
   ...

The first one being redundant for the reasons I have already stated.

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