TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Humbertogo on February 24, 2006, 04:47:41 AM

Title: converting characters to Title Case
Post by: Humbertogo on February 24, 2006, 04:47:41 AM
How to converting  characters  to Title Case

"SAMPLE" ==> "Sample"

Thanks :roll:
Title: Re: converting characters to Title Case
Post by: hudster on February 24, 2006, 06:42:20 AM
in mtext editor, right click, change chase. but I think that was too easy and not what your looking for
Title: Re: converting characters to Title Case
Post by: Kerry on February 24, 2006, 06:49:02 AM
;;
;;  Tcase.lsp - Changes case of selected text, attdefs, attributes, dimension text,
;;               mtext, arcalignedtext and rtext.
;;                   
;;
;;  Copyright © 1999 by Autodesk, Inc.

Express Tools Folder ...
Title: Re: converting characters to Title Case
Post by: MP on February 24, 2006, 09:17:32 AM
...Express Tools Folder ...

Funny, when I read your post I saw "Express Tools Fodder".

 :-D
Title: Re: converting characters to
Post by: Humbertogo on February 24, 2006, 09:40:55 AM
Don't need to converting Mtext i need to convert a string to Title Case
Title: Re: converting characters to Title Case
Post by: zoltan on February 24, 2006, 09:55:05 AM
I just whipped this up:
Code: [Select]
(Defun StrTitleCase ( STR )
 (StrCat (StrCase (SubStr STR 1 1)) (StrCase (SubStr STR 2) T) )
)

It only works for one word.

hold on..
Title: Re: converting characters to Title Case
Post by: zoltan on February 24, 2006, 10:13:31 AM
Code: [Select]
(Defun StrTitleCase ( STR )
 (StrCat (StrCase (SubStr STR 1 1)) (StrCase (SubStr STR 2) T) )
)

;;Splits a string into a list of strings based on a delimiter string
(Defun StrSplt ( str del / s cnt lastdel return)
 (SetQ s str)
 (While s
  (SetQ cnt 1)
  (While (<= cnt (StrLen s))
   (If (= (SubStr s cnt (StrLen del)) del)
    (ProgN
     (SetQ lastdel cnt)
     (SetQ cnt (1+ cnt))
    )
    (SetQ cnt (1+ cnt))
   )
  )
  (If lastdel
   (ProgN
    (SetQ return (Cons (Substr s (+ lastdel (StrLen del))) return))
    (SetQ s (SubStr s 1 (1- lastdel)))
    (SetQ lastdel nil)
   )
   (SetQ return (cons s return)
         s      nil
   )
  )
 )
 return
)

;;Returns a sentence string given a list of strings and
;;an optional length
(Defun Sentence ( lst len / cnt return)
 (SetQ cnt 0
       return ""
 )
 (Repeat (Cond (len) (T (Length lst)))
  (SetQ return (StrCat return (Nth cnt lst) " "))
  (SetQ cnt (1+ cnt))
 )
 (SetQ return (VL-String-Trim " " return))

 return
)

(Defun TitleCase ( STR lstLOWER lstUPPER / sWord lstSent )
 (ForEach sWord (StrSplt STR " ")
  (Cond
   ((If lstLOWER
     (Member sWord lstLOWER )
     nil
    )
    (SetQ lstSent (Cons (StrCase sWord T) lstSent) )
   )
   ((If lstUPPER
     (Member sWord lstUPPER)
     nil
    )
    (SetQ lstSent (Cons (StrCase sWord) lstSent) )
   )
   (T
    (SetQ lstSent (Cons (StrTitleCase sWord) lstSent) )
   )
  )
 )

 (Sentence (Reverse lstSent) nil)
)

_$ (TitleCase "this is a test of how this works" '("a" "of") nil)
_1_$
"This Is a Test of How This Works"

It still needs to take into account punctuation, so words ending in punctuation will match words in the list.  And I guess the first word should always be caps, even if it is in the lower case list.

You can tell I have nothing to do at work today.
Title: Re: converting characters to Title Case
Post by: MP on February 24, 2006, 10:17:18 AM
This discussion (http://www.theswamp.org/forum/index.php?topic=2953.msg37000#msg37000) includes a "proper" function that may, or may not be illuminating / intersting / useful.
Title: Re: converting characters to Title Case
Post by: zoltan on February 24, 2006, 10:28:21 AM
This discussion (http://www.theswamp.org/forum/index.php?topic=2953.msg37000#msg37000) includes a "proper" function that may, or may not be illuminating / intersting / useful.

Hey wow, that function does EXACTLY what mine does.  Amazing!  :-D
Title: Re: converting characters to Title Case
Post by: Andrea on February 24, 2006, 10:37:51 AM
Code: [Select]
(setq string "you ARE The King")
(setq string (strcat (strcase (substr string 1 1)) (strcase (substr string 2) 1)))
Title: Re: converting characters to Title Case
Post by: zoltan on February 24, 2006, 10:50:03 AM
_$ (setq string "you ARE The King")
"you ARE The King"
_$ (setq string (strcat (strcase (substr string 1 1)) (strcase (substr string 2) 1)))
"You are the king"
_$

Title Case is Every Word in the Sentance, Except For a Few

This is Sentance case.

I'm glad everyone is reading the WHOLE thread here.  :roll:
Title: Re: converting characters to Title Case
Post by: hudster on February 24, 2006, 10:56:57 AM
This is Sentance case.

I'm glad everyone is reading the WHOLE thread here.  :roll:


Maybe they are checking their spelling instead. :-D
Title: Re: converting characters to Title Case
Post by: zoltan on February 24, 2006, 11:13:58 AM
Yea yea... My spelling sucks!  :oops:
Title: Re: converting characters to Title Case
Post by: Andrea on February 24, 2006, 01:38:11 PM
 :pissed:

I've used the Joe Burke routine..  http://www.theswamp.org/forum/index.php?topic=8817.0

Code: [Select]
(defun String2List (str pat / i j n lst)
  (cond
    ((/= (type str)(type pat) 'STR))
    ((= str pat)'(""))
    (T
      (setq i 0 n (strlen pat))
      (while (setq j (vl-string-search pat str i))
        (setq lst (cons (substr str (1+ i)(- j i)) lst)
              i (+ j n)
        )
      )
      (reverse (cons (substr str (1+ i)) lst))
    )
  )
) ;end

(setq TL (string2list "yOu are tHE kING" " "))
(setq 2w "")
(foreach n TL
  (setq 1w (strcat (strcase (substr n 1 1))(strcase (substr n 2) 1)))
  (setq 2w (strcat 2w 1w " "))
)

(alert (vl-string-right-trim " " 2w))
Title: Re: converting characters to Title Case
Post by: Humbertogo on February 25, 2006, 04:40:24 AM
thanks  
Title: Re: converting characters to Title Case
Post by: Peter Jamtgaard on February 25, 2006, 05:33:14 PM
Maybe

Code: [Select]
(defun TitleCase (strSentence / intCount intItem lstCharacters lstCharacters2)
 (setq strSentence (strcase strSentence 'T)
       intCount 0)
 (repeat (length (setq lstCharacters (vl-string->list strSentence)))
  (setq intItem (nth intCount lstCharacters))
  (if (and (or (= intCount 0)
               (= (nth (1- intCount) lstCharacters) 32)
           )
           (> intItem 96)
           (< intItem 123)
      )
   (setq intItem (- intItem 32))     
  )
  (setq lstCharacters2 (cons intItem lstCharacters2)
        intCount       (1+ intCount)
  )
 )
 (print (reverse lstCharacters2))
 (vl-list->string (reverse lstCharacters2))
)