Text case changing

JonathanC at ag.nsw.gov.au JonathanC at ag.nsw.gov.au
Thu Jun 3 10:17:39 EDT 2004


Bill Vlahos wrote on 03/06/2004 02:44:05 PM:

> Revolution has the toUpper and toLower command which will convert an 
> entire string. Is there something similar for capitalizing just the 
> first letter as a built in function? I've come up with something that 
> works but seems kind of hokey. Does anyone have a more elegant solution 
> for this?
> 
> Just to make life a little more complicated, there are some names like 
> O'Malley or MacKeif or McBeth where it isn't just the first character. 
> My application does not have to deal with these situations but a 
> general solution would.

OK, maybe it's not that elegant, but it works for me:

(Sorry, it doesn't deal with "O'Malley" etc., but it does deal with 
'little words' in titles and variants of the word "I". 
So, e.g.
changeCase("THE NAME OF THE ROSE","title") => "The Name of the Rose"
and
changeCase("I SAID I'D COME TOMORROW","sentence") => "I said I'd come 
tomorrow.")

(Note: some of the characters in these handlers may not make it intact 
because they are Macintosh curly quotes.)

function changeCase theText,theMode
  -- theMode = "upper", "lower", "sentence" or "title"
  if theMode = "upper" then return toUpper(theText)
  else if theMode = "lower" then return toLower(theText)
  else
    set cursor to watch
    put "" into newText
    repeat with L=1 to the number of lines in theText
      put line L of theText into theLine
      repeat with i=1 to the length of theLine
        get char i of theLine
        if theMode = "Sentence" then
          if isStartOfSentence(theLine,i) or isTheWordI(theLine,i) then 
put toUpper(it) after newText
          else put toLower(it) after newText
        else
          -- theMode = "title"
          if (isStartOfWord(theLine,i) and not isLittleWord(theLine,i)) ¬
          or isStartOfSentence(theLine,i) or isTheWordI(theLine,i) then
            put toUpper(it) after newText
          else put toLower(it) after newText
        end if
      end repeat
      if L<the number of lines in theText then put return after newText
    end repeat
    return newText
  end if
end changeCase

function isStartOfSentence str,charNum
  return charNum=1 ¬
  or char charNum-2 to charNum-1 of str = ". " ¬
  or char charNum-3 to charNum-1 of str = ". (" ¬
  or char charNum-3 to charNum-1 of str = ". " & quote ¬
  or char charNum-3 to charNum-1 of str = ". ?" ¬
  or char charNum-3 to charNum-1 of str = ". ?" ¬
  or (charNum=2 and char 1 of str is in "(??" & quote)
end isStartOfSentence

function isStartOfWord str,charNum
  return charNum=1 ¬
  or char charNum-1 of str = " " ¬
  or char charNum-2 to charNum-1 of str = " (" ¬
  or char charNum-2 to charNum-1 of str = " " & quote ¬
  or char charNum-2 to charNum-1 of str = " ?" ¬
  or char charNum-2 to charNum-1 of str = " ?" ¬
  or (charNum=2 and char 1 of str is in "(??" & quote)
end isStartOfWord

function isLittleWord str,charNum
  put the number of words in char 1 to charNum of str into wordNum
  return space & word wordNum of str & space is in " a an and are at be by 
do for from if in is it its of on or the to with "
end isLittleWord

function isTheWordI str,charNum
  if quote is in str then put substitute(str,quote,tab) into str -- Rev 
regards a string enclosed in quotes as one word
  put the number of words in char 1 to charNum of str into wordNum
  get word wordNum of str
  if "i" is not in it then return false
  if it = "i" then return true
  if space & it & space is in " i i'm i?m i'd i?d i've i?ve i'll i?ll " 
then return true
  put offset("i",it) into pos1
  if space & (char pos1 to pos1+2 of it) & space is in " i'm i?m i'd i?d " 
then put pos1+2 into pos2
  else if space & (char pos1 to pos1+3 of it) & space is in " i've i?ve 
i'll i?ll " then put pos1+3 into pos2
  else put pos1 into pos2
  repeat with i=1 to pos1-1
    if char i of it is in "abcdefghijklmnopqrstuvwxyz" then return false
  end repeat
  repeat with i=pos2+1 to the length of it
    if char i of it is in "abcdefghijklmnopqrstuvwxyz" then return false
  end repeat
  return true
end isTheWordI

Regards,

Jonathan Cooper
Manager of Information / Website
Art Gallery of New South Wales
Sydney, Australia
http://www.artgallery.nsw.gov.au



More information about the use-livecode mailing list