Capitalizing the first character/letter in a word

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Barkandgargle
Posts: 9
Joined: Wed Feb 16, 2011 5:32 pm

Capitalizing the first character/letter in a word

Post by Barkandgargle » Wed Feb 16, 2011 5:44 pm

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...

BvG
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1239
Joined: Sat Apr 08, 2006 1:10 pm
Contact:

Re: Capitalizing the first character/letter in a word

Post by BvG » Wed Feb 16, 2011 6:14 pm

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.
Various teststacks and stuff:
http://bjoernke.com

Chat with other RunRev developers:
chat.freenode.net:6666 #livecode

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Capitalizing the first character/letter in a word

Post by dunbarx » Wed Feb 16, 2011 7:15 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Capitalizing the first character/letter in a word

Post by dunbarx » Wed Feb 16, 2011 7:17 pm

Check out the thread in this forum called "saving contents of entire stack".

Craig Newman

BvG
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1239
Joined: Sat Apr 08, 2006 1:10 pm
Contact:

Re: Capitalizing the first character/letter in a word

Post by BvG » Wed Feb 16, 2011 9:55 pm

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.
Various teststacks and stuff:
http://bjoernke.com

Chat with other RunRev developers:
chat.freenode.net:6666 #livecode

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Capitalizing the first character/letter in a word

Post by dunbarx » Wed Feb 16, 2011 10:33 pm

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

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Capitalizing the first character/letter in a word

Post by jmburnod » Thu Feb 17, 2011 5:07 pm

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
https://alternatic.ch

Barkandgargle
Posts: 9
Joined: Wed Feb 16, 2011 5:32 pm

Re: Capitalizing the first character/letter in a word

Post by Barkandgargle » Thu Feb 17, 2011 5:35 pm

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.

Post Reply