A string is a group of characters surrounded by quotation marks. Within quoted strings the backslash (\) character allows control characters (or escape codes) to be included.
The alphabetic characters of a string can be converted to uppercase or lowercase with the strcase function. 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."
(strcase "This is a TEST." T) "this is a test."
You can combine multiple strings into a single string value with the strcat function. This is useful for placing a variable string within a constant string, such as an error message or note in a drawing. The following code example sets a variable to a string value and then uses strcat to insert that string between two other strings.
(setq str "BIG") (setq bigstr (strcat "This is a " str " test.")) "This is a BIG test."
The substr function allows you to return a portion of a string. This function requires two arguments and has one optional argument. The first argument is a string and the second argument is an integer that represents the start character of the string that you want to return as the substring. If the third argument is not provided, substr returns all characters including and following the specified start character.
(substr "Welcome to AutoLISP" 12) "AutoLISP"
If want to return a substring that is at the beginning or middle of the string provided to the substr function, you can specify an integer for the third argument that represents the number of characters that should be returned. For example, the following example code returns the first 7 characters of the provided string:
(substr "Welcome to AutoLISP" 1 7) "Welcome"
Often, when working with a string you might not know how long it is but might know the start position of the substring you want to return. The strlen function returns the number of characters (including spaces) in a string.
(setq filnam "bigfile.txt") (strlen filnam) 11
The following example code returns all the characters in a filename except the last four (the period and the three-letter extension). This is done by using strlen to get the length of the string and subtract 4 from that value. Then substr is used to specify the first character of the substring and its length.
(setq newlen (- (strlen filnam) 4)) 7 (substr filnam 1 newlen) "bigfile"
(substr filnam 1 (- (strlen filnam) 4)) "bigfile"
Finding and replacing text can be helpful in updating notes or part numbers. The vl-string-search function allows you to locate a pattern within a string, and return the start position as an integer of the first instance of the specified pattern. If the function returns an integer, you can then use that as the starting position for another search to make sure there is not more than one instance of the pattern in the string.
The vl-string-subst function can be used to replace text within a string. Similar to the vl-string-search function, it can only identify the first instance of a specified pattern. You can use the vl-string-search function after replacing a text string to see if another instance of a pattern is contained in the string returned by vl-string-subst.
(setq note "All door openings are [WIDTH] unless otherwise noted.") "All door openings are [WIDTH] unless otherwise noted." (setq position (vl-string-search "[WIDTH]" note)) 22 (setq revised-note (vl-string-subst "36\"" "[WIDTH]" note position)) "All door openings are 36\" unless otherwise noted." (prompt revised-note)(princ) All door openings are 36" unless otherwise noted.