Visual LISP, AutoLISP and General Customization

Reply
Mentor
aqdam1978
Posts: 265
Registered: ‎11-05-2008
Message 1 of 15 (764 Views)
Accepted Solution

Replacing substr(s) in a string variable

764 Views, 14 Replies
02-18-2013 11:11 AM

Hi,

I write a code to replace strings in one string.
for example
str="AutoDesk, AutoCAD, 2013, Electrical, 64Bit"

patt=", "

NewStr=" - "

==> result: "AutoDesk - AutoCAD - 2013 - Electrical - 64Bit"

 

 

;;replaces NewStr instead of Patt(s) in Str string
(defun ReplaceStr (NewStr Patt Str)
(While (> (vl-string-search ptt str) 0)
	(setq Str (vl-string-subst NewStr patt Str)) 
);;while
str
)
  
; usage
;(ReplaceStr " - " ", " "AutoDesk, AutoCAD, 2013, Electrical, 64Bit")

  

==> result= ; error: bad argument type: (or stringp symbolp): nil

 

What is my mistake(s)?

 

Thanks

 

 

Valued Contributor
Rtogores
Posts: 84
Registered: ‎02-07-2001
Message 2 of 15 (753 Views)

Re: Replacing substr(s) in a string variable

02-18-2013 12:25 PM in reply to: aqdam1978

A similar function that works is included in Chapter 5 (Listing 5.12) of my book on AutoLISP/Visual LISP.

This is the code:

(defun replace (new old string)
  (while (vl-string-search old string)
    (setq string (vl-string-subst new old string))
  ) ;_ end of  while
) ;_ end of  defun
;;;Listing 5.12. Replacement of characters in a string.

But, if you only want to replace the "," with a  "-" without including a space before the dash you can use:

_$ (vl-string-translate "," "-" "AutoDesk, AutoCAD, 2013, Electrical, 64Bit")
"AutoDesk- AutoCAD- 2013- Electrical- 64Bit"
_$ 

 

More info in my blog lispexpert.blogspot.com.

Mentor
aqdam1978
Posts: 265
Registered: ‎11-05-2008
Message 3 of 15 (749 Views)

Re: Replacing substr(s) in a string variable

02-18-2013 12:31 PM in reply to: Rtogores

Thank you for your answer, But what was my mistake? why my lisp did not work?

 

Thanks,

 

 

*Expert Elite*
hmsilva
Posts: 2,639
Registered: ‎12-17-2004
Message 4 of 15 (744 Views)

Re: Replacing substr(s) in a string variable

02-18-2013 12:37 PM in reply to: aqdam1978

aqdam,

your code works, just miss an "a" in the second "patt"

 

;;replaces NewStr instead of Patt(s) in Str string
(defun ReplaceStr (NewStr Patt Str)
(While (> (vl-string-search patt str) 0)
	(setq Str (vl-string-subst NewStr patt Str)) 
);;while
str
)
  
; usage
;(ReplaceStr " - " ", " "AutoDesk, AutoCAD, 2013, Electrical, 64Bit")

 

 

Henrique

Valued Contributor
Rtogores
Posts: 84
Registered: ‎02-07-2001
Message 5 of 15 (734 Views)

Re: Replacing substr(s) in a string variable

02-18-2013 12:45 PM in reply to: aqdam1978

Yes, only a typo, ptt for patt.

But the point is that the function can be simplified, as vl-string-search will return nil when no further ocurrences are found, and the value returned by the last setq will be the value returned by the function.

Mentor
aqdam1978
Posts: 265
Registered: ‎11-05-2008
Message 6 of 15 (731 Views)

Re: Replacing substr(s) in a string variable

02-18-2013 12:50 PM in reply to: Rtogores

aaaah, yes,

you are right! also, I can simplify it as you said:

 

(defun ReplaceStr (NewStr Patt Str)
	(While (vl-string-search patt str)
		(setq Str (vl-string-subst NewStr patt Str)) 
	);;while
)

 Thanks

Mentor
aqdam1978
Posts: 265
Registered: ‎11-05-2008
Message 7 of 15 (700 Views)

Re: Replacing substr(s) in a string variable

02-19-2013 05:28 PM in reply to: hmsilva

Hi,

I have a problem with this code:

(defun ReplaceStr (NewStr Patt Str)
	(While (vl-string-search patt str)
	(setq Str (vl-string-subst NewStr patt Str)) 
	);;while
str ;;in case of no patt exist in str, just return str itself
)

 as you know in prompt command we have:

(prompt "c:\\") (princ) ==> c:\
(prompt "c:\\\\")(princ) ==> c:\\

 

so, why this code does not work and AutoCAD goes to "no response" situation?

(prompt (ReplaceStr "\\\\" "\\" "c:\\"))(princ)  ;;AutoCAD will be freeze!

 

Thanks,

 

*Expert Elite*
pbejse
Posts: 2,406
Registered: ‎11-24-2009
Message 8 of 15 (692 Views)

Re: Replacing substr(s) in a string variable

02-19-2013 08:03 PM in reply to: aqdam1978

aqdam1978 wrote:

Hi,

 

(prompt (ReplaceStr "\\\\" "\\" "c:\\"))(princ)  ;;AutoCAD will be freeze!

 

Thanks,

 


Because of an endless loop aqdam1978  (vl-string-search patt str) will never evaluate to nil 

 

Try this instead  using vl-string-subst to do a find and replace in external file by AlessiMarc'Antonio

 

HTH

Valued Contributor
Rtogores
Posts: 84
Registered: ‎02-07-2001
Message 9 of 15 (674 Views)

Re: Replacing substr(s) in a string variable

02-20-2013 02:49 AM in reply to: aqdam1978
so, why this code does not work and AutoCAD goes to "no response" situation?

(prompt (ReplaceStr "\\\\" "\\" "c:\\"))(princ)  ;;AutoCAD will be freeze!

In this case you are just adding the same character to the string, not replacing it with a different one, so vl-string-search will always find that character and add a couple of new occurrences in each loop, so it'll never conclude.
To catch these kind of error I would suggest you set a breakpoint in your code and open a watch window to inspect the *LAST-VALUE*. Then call the function in the VLISP Editor and use the Step Into button to evaluate each expression. For a video explaining this way to inspect a program's workings, you can see Module 5 of my AU class: http://au.autodesk.com/?nd=class&session_id=10671.
In this case you do not need your replacement function.(vl-string-subst "\\\\" "\\" "C:\\") will do.




Valued Contributor
Rtogores
Posts: 84
Registered: ‎02-07-2001
Message 10 of 15 (666 Views)

Re: Replacing substr(s) in a string variable

02-20-2013 04:01 AM in reply to: Rtogores

Rtogores wrote: For a video explaining this way to inspect a program's workings, you can see Module 5 of my AU class: http://au.autodesk.com/?nd=class&session_id=10671.

Also (better) in Module 3!


 

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community