String Handling
 
 
 

AutoLISP provides functions for working with string values. For example, the strcase function returns the conversion of all alphabetic characters in a string to uppercase or lowercase. It accepts two arguments: a string and an optional argument that specifies the case in which the characters are returned. If the optional second argument is omitted, it evaluates to nil and strcase returns the characters converted to uppercase.

_$ (strcase "This is
a TEST.")
"THIS IS A TEST." 

If you provide a second argument of T, the characters are returned as lowercase. AutoLISP provides the predefined variable T to use in similar situations where a non-nil value is used as a type of true/false toggle.

_$ (strcase "This is
a TEST." T)
"this is a test." 

The strcat function combines multiple strings into a single string value. This is useful for placing a variable string within a constant string. The following code sets a variable to a string value and then uses strcat to insert that string into the middle of another string.

_$ (setq str "BIG")
(setq bigstr (strcat "This is a " str " test."))
"This is a BIG test."

If the variable bigstr is set to the preceding string value, you can use the strlen function to find out the number of characters (including spaces) in that string.

_$ (strlen bigstr)
19

The substr function returns a substring of a string. It has two required arguments and one optional argument. The first required argument is the string. The second argument is a positive integer that specifies the first character of the string you want to include in the substring. If the third argument is provided, it specifies the number of characters to include in the substring. If the third argument is not provided, substr returns all characters including and following the specified start character.

As an example, you can use the substr function to strip off the three-letter extension from a file name (note that you can actually use the vl-file-name-base function to do this). First, set a variable to a file name.

_$ (setq filnam "bigfile.txt")
"bigfile.txt"

You need to get a string that contains all characters except the last four (the period and the three-letter extension). Use strlen to get the length of the string and subtract 4 from that value. Then use substr to specify the first character of the substring and its length.

_$ (setq newlen (- (strlen
filnam) 4))
7 
_$ (substr filnam 1
newlen)
"bigfile" 

If your application has no need for the value of newlen, you can combine these two lines of code into one.

_$ (substr filnam 1
(- (strlen filnam) 4))
"bigfile" 

Additional string-handling functions are listed in AutoLISP Function Synopsis under the heading String-Handling Functions These functions are described in the AutoLISP Reference.

AutoLISP also provides a number of functions that convert string values into numeric values and numeric values into string values. These functions are discussed in Conversions.