Page 1 of 1

Capitalizing the first character/letter in a word

Posted: Wed Feb 16, 2011 5:44 pm
by Barkandgargle
What's the easiest most direct way to change only the first character in a word to uppercase, leaving the rest to lower case?

Thanks in advance...

Re: Capitalizing the first character/letter in a word

Posted: Wed Feb 16, 2011 6:14 pm
by BvG

Code: Select all

put toUpper(char 1 of word 1 of field "test") into char 1 of word 1 of field "test"
read the entries for:
toUpper
word
wordoffset


note that words are not what you'd assume them to be in LiveCode, so depending on the input text, you'd need to adapt.

Re: Capitalizing the first character/letter in a word

Posted: Wed Feb 16, 2011 7:15 pm
by dunbarx
This is called Titletext. But do you need the first char of each word, or the first char of each sentence?

The first is easy. If the second, there was a thread a while back that gave several
solutions and opinions. It took careful parsing, watching out for periods, decimal points, etc. Check the archives.

Bjornke, what did you mean when you said words are not what you assume them to be?

Craig Newman

Re: Capitalizing the first character/letter in a word

Posted: Wed Feb 16, 2011 7:17 pm
by dunbarx
Check out the thread in this forum called "saving contents of entire stack".

Craig Newman

Re: Capitalizing the first character/letter in a word

Posted: Wed Feb 16, 2011 9:55 pm
by BvG
words in LC are either delimited by space, return or tab, or they're within quotes. the part about quotes does trip people up the first time. additionally, punctiation is ignored when considering words, so finding out where a sentence starts can be hard too.

Re: Capitalizing the first character/letter in a word

Posted: Wed Feb 16, 2011 10:33 pm
by dunbarx
Bjornke.

Oh, I see what you meant now. Yes, words are the most slippery of the chunks. Do you remember the long discussion about why "A B" (an "A", two spaces, and a "B") was two words and not three?

Craig

Re: Capitalizing the first character/letter in a word

Posted: Thu Feb 17, 2011 5:07 pm
by jmburnod
Hi Barkandgargle,

I write a little function it make the job

Code: Select all

on mouseUp
   put fld "myFld" into mytext
   put FirstCharHi(mytext)
end mouseUp

function FirstCharHi ptext
   put numtochar(34) into tsearch -- 34 = chartonum of ",maeby you have others
   put "•" into tNewChar
   put replacetext(pText,tsearch,tNewChar) into LeT
   repeat with i = 1 to the num of words of LeT
      put word i of LeT into UnW
      if char 1 of UnW = "•" then
       put 2 into LeFirstChar
      else
         put 1 into LeFirstChar
      end if
      put char LeFirstChar of UnW into LeC
      put chartonum(LeC) into LeCTN
      if LeCTN > 96 and LeCTN <123 then
        subtract 32 from LeCTN
         put numtochar(LeCTN) into char LeFirstChar of word i of LeT
         wait 1 milliseconds
      end if
   end repeat
  put replacetext(LeT,tNewChar,tsearch) into LeT
   return LeT
end FirstCharHi
Best regards

Jean-Marc

Re: Capitalizing the first character/letter in a word

Posted: Thu Feb 17, 2011 5:35 pm
by Barkandgargle
BvG wrote:

Code: Select all

put toUpper(char 1 of word 1 of field "test") into char 1 of word 1 of field "test"
Thanks for your help. The code above is what I thought should work, but I didn't have it exactly quite right. But this now works. Appreciate it.