The AutoLisp Intermediate Tutorial


String Functions

Functions  - substr   strcase    strlen     strcat   Example Programs


substr - This function retrieves part of a string.   

                       Syntax : (substr  "string"   startPoint    numberOfCharacters)

                    "string" - any valid string or variable representing a string.  If you use a variable name omit the quotes.

                    startPoint - an integer or variable representing an integer that corresponds to the position in the string to                                           start the substring at.  The first character is character 1.

                     numberOfCharacters - an integer or variable representing an integer that corresponds to the length of                                           string to return.  Length being the number of characters.

                Returns a partial string starting at the character represented by startPoint and ending at the character                                 represented by the numberOfCharacters or the end of the string.  Whichever comes first..


               (substr "Jeff Sanders" 1 6) would return "Jeff S"

               (substr "Jeff Sanders" 6 3) would return "San"

                (substr "Jeff Sanders" 64 456) would return ""

                (substr "Jeff Sanders" -4 3) would return "Error: Bad argument"  [No negative character                                                                                    positions]

                (substr "Jeff Sanders" 4 -3) would return "Error: Bad argument"  [No negative string lengths]

                (substr "Jeff" 6 4) would return "" 

                (substr "" 9 3) would return "" 

                (substr "Jeff Sanders" 9) would return "ders" 

                 If you omit the last parameter, the remainder of the string from the startPoint will be returned..

 

strcase - This function converts to upper or lower case.

                  Syntax   :  (strcase "string" flag)

                 "string" - any valid string or variable representing a string.  If you use a variable omit the quotes.

                        flag - a flag that sets uppercase or lowercase.  

                                     flag = T or nil 

                                     T = True  nil = False

                                     T = LowerCase      nil or empty = UpperCase

                  Returns a string that is either upper or lower case depending on the flag setting.

                 (strcase "Jeff" T) returns "jeff"

                 (strcase "Jeff" nil) returns "JEFF"

                 (strcase "123a" nil) returns "123A"

                 (strcase "" nil) returns ""

                 (strcase 123 T) returns "Error: Bad Argument Type"            [123 is an integer, not a string]

                 (strcase 1.0 nil) returns "Error: Bad Argument Type"        [1.0 is a real number, not a string]

                 (strcase "Jeff") returns "JEFF"

                  Omitting the flag is the same as having the flag set to nil or false.

 

strlen - This function returns the length of a string in characters.

              Syntax :   (strlen "string")

               "string" - any valid string or variable representing a string.  If you use a variable omit the quotes.

               Returns an integer that represents the length of the string.  The length is the number of characters.

                 (strlen "Jeff") returns 4

                 (strlen "Jeff Sanders") returns 12

                 (strlen "") returns 0

                 (strlen 123) returns "Error: Bad Argument Type      [123 is an integer, not a string]

                 (strlen 1.0) returns "Error: Bad Argument Type      [1.0 is a real number, not a string]

                 (strlen (list 1.0 2.0)) returns "Error: Bad Argument Type  [(list 1.0 2.0) is a list, not a string]

 

strcat - This function adds multiple strings together to form one string.

               Syntax : (strcat "string1" "string2" "string3" ....ect. )

               "string1" - any valid string or variable representing a string.  If you use a variable omit the quotes.

               Returns a string that includes all of the strings in the list.

                 (strcat "Jeff" "Sanders") returns "JeffSanders"

                 (strcat "Jeff " "Sanders") returns "Jeff Sanders"

                 (strcat "" "56" "Jeff" "abcdefg") returns "56Jeffabcdefg"

                 (strcat "" 5 "Jeff") returns "Error: Bad argument type".  [5 is an integer, not a string]

                 (strcat "" 3.1 "Jeff") returns "Error: Bad argument type".  [3.1 is a real number, not a string]

                  This function can only accept strings  or a variable representing a string as it's arguments. (parameters)

End of String Functions


Example Program 1:

(defun C:myProg()

  (setq str1 "Jeffery")

  (setq str2 "P")

  (setq str4(substr str1 1 4))          ;sets str4 to 1st 4 characters of str1

  (princ str4)                          ;Prints "Jeff" to the command line.

  (setq str4(strcase str4))             ;Sets str4 to uppercase

  (princ str4)                          ;Prints "JEFF" to the command line.

  (setq int1(strlen str4))              ;Sets int1 to 4 (length of string)

  (princ int1)                          ;Prints 4 to the command line.

  (setq str5 (strcat str4 " " str2))    ;sets str5 to "JEFF P"

  (princ str5)                          ;Prints "JEFF P" to the command line.

  (princ)                               ;clean exit (supresses echo)

)                                       ;close the program

Command: (load "myProg")<enter>

Command: myProg<enter>

Command: Jeff4JEFF P

 

Program Example 2:

(defun C:NameMix()

(setq strName(getstring T "\n Enter your name: "))

(setq keepGoing 1)

(while (<= keepGoing (strlen strName))

  (setq char1(substr strName 1 keepGoing))

  (setq char1 (strcase char1))

  (setq strName2(strcat char1 (substr strName (+ keepGoing 1))))

  (setq keepGoing(+ keepGoing 1))

  (princ (strcat "\n " strName2))

)

(princ "\n Program Complete.")

(princ)

)

Command: (load "namemix")<enter>

Command: namemix<enter>

Command: Enter Your Name: jeff<enter>

Command: Jeff

Command: JEff

Command: JEFf

Command: JEFF

Command: Program Complete.


AutoLisp Intermediate Tutorial

AutoLisp Tutorial Home