Insert character two spaces from end of text

Insert character two spaces from end of text

Anonymous
Not applicable
1,883 Views
9 Replies
Message 1 of 10

Insert character two spaces from end of text

Anonymous
Not applicable

I'm very new to AutoCAD and Lisp, but pretty experienced in Python.

I have a text string in AutoCAD, and want to insert a '+' character two spaces from the end of the text.

Ex. 'ABCDEFG' > 'ABCDE+FG', 'AB23567890J' > 'AB2356789+OJ', etc.

This is simple enough to do in Python, VB, C++, etc, but can't figure out how to do it in Lisp. I found the code 'Insert Nth' on Lee Mac's site. That code appears to be limited to always inserting the character at the same position from the start, whereas mine is the same position from the end. Conceptually this seems simple, but have no clue how to do so in Lisp. Any help? Thanks.

0 Likes
Accepted solutions (2)
1,884 Views
9 Replies
Replies (9)
Message 2 of 10

Shneuph
Collaborator
Collaborator
Accepted solution
(Setq str "this is my string")
(strcat
  (substr str 1 (- (strlen str) 1))
  "+"
  (substr str (- (strlen str) 1))
  );strcat
---sig---------------------------------------
'(83 104 110 101 117 112 104 64 71 109 97 105 108 46 99 111 109)
Message 3 of 10

devitg
Advisor
Advisor

Try it , please .

No error  check, no nothing , just crude LISP

 

(setq string (cdr (Assoc 1 (entget (ssname(ssget "+.:S"  (list (cons 0  "text"  )))0)))))

(setq str-len (STRLEN string))

(setq pos (-  str-len 1))
;;Copyright © http://autolisp.mapcar.net/strtok.html

(DEFUN STR-INSERT  (STR NEU POS /)
  (STRCAT
    (SUBSTR STR 1 (1- POS))
    NEU
    (SUBSTR STR POS)
    )
  )

(Setq string+nn (STR-INSERT string "+" pos))
0 Likes
Message 4 of 10

pbejse
Mentor
Mentor
Accepted solution

@Shneuph wrote:
...
  (substr str 1 (- (strlen str) 1))
...

"this is my strin+ng"  

 

Shouldn't that be:

(substr str 1 (- (strlen str) 2))

variant

((lambda (n)
	   (strcat
	     (substr str 1 (- n 2))
	     "+"
	     (substr str (1- n))
	   )
	 )
	  (strlen str)
	)
Message 5 of 10

Shneuph
Collaborator
Collaborator

Yes, you are correct.  Good catch.

---sig---------------------------------------
'(83 104 110 101 117 112 104 64 71 109 97 105 108 46 99 111 109)
0 Likes
Message 6 of 10

Anonymous
Not applicable

Thanks all, works great!

0 Likes
Message 7 of 10

Anonymous
Not applicable

Thanks devitg, get an error though:

Bad argument type, numberp: nil

0 Likes
Message 8 of 10

devitg
Advisor
Advisor

I'll check it. 

0 Likes
Message 9 of 10

Shneuph
Collaborator
Collaborator

Looks like the extra space here:

 

(setq pos (-  str-len 1))

 

 

Edit:
Nope maybe that's not it.

---sig---------------------------------------
'(83 104 110 101 117 112 104 64 71 109 97 105 108 46 99 111 109)
0 Likes
Message 10 of 10

devitg
Advisor
Advisor

Hi , as per me , LISp allow extra spaces 

 

 

(setq       pos       (-       str-len     1))


But not  a extra space as follow 

 

(setq       pos       (-       str  -len     1))

As it  will search for 2 variables 

str 
-len