Text Case Functions
Here I demonstrate a selection of functions to convert a text string to various text cases or formats.
The AutoLISP language already includes the strcase function to convert a string to uppercase or lowercase, so, for completeness I would add these two redundant functions:
(defun LM:UpperCase ( s ) (strcase s)) (defun LM:LowerCase ( s ) (strcase s t))
The functions I offer below allow conversion of a text string to Sentence Case, Title Case, and finally, the case of each character in a string may be switched using the Toggle Case function.
Sentence Case
Select all
;; Sentence Case - Lee Mac ;; Returns the supplied string converted to Sentence Case (defun LM:SentenceCase ( s / f ) (vl-list->string (mapcar (function (lambda ( a b c ) (if (or f (= 46 a)) (progn (setq f (= 32 b)) b) c) ) ) (cons 46 (vl-string->list s)) (vl-string->list (strcase s)) (vl-string->list (strcase s t)) ) ) )
Example Function Call
_$ (LM:SentenceCase "this is a sentence. this is another sentence.") "This is a sentence. This is another sentence."
Title Case
Select all
;; Title Case - Lee Mac ;; Returns the supplied string converted to Title Case (defun LM:TitleCase ( s ) (vl-list->string (mapcar (function (lambda ( a b c ) (if (= 32 a) b c)) ) (cons 32 (vl-string->list s)) (vl-string->list (strcase s)) (vl-string->list (strcase s t)) ) ) )
Example Function Call
_$ (LM:TitleCase "this is a title") "This Is A Title"
Toggle Case
Select all
;; Toggle Case - Lee Mac ;; Returns the supplied string with the text case of each character switched (defun LM:ToggleCase ( s ) (vl-list->string (mapcar (function (lambda ( a b c ) (if (< 96 a 123) b c)) ) (vl-string->list s) (vl-string->list (strcase s)) (vl-string->list (strcase s t)) ) ) )
Example Function Call
_$ (LM:ToggleCase "This is a ToggleCase Example.") "tHIS IS A tOGGLEcASE eXAMPLE."