Go Back   Technology Questions > Software Questions > Operating System Questions > Vista Community > Windows Vista

Windows Vista Discuss the different versions of Windows Vista, Fuji, or Vienna

Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old 07-24-2008, 03:51 PM
kevpan815's Psychiatrist
Tablet PC Guest
 
Posts: n/a
How to capitalize the first letter of each word in a string. Just FYI

Say we have a string "This is my String" - we want to capitalize the first
letter of each word so that my string becomes "This Is My String". My first
attempt was to use the capitalize method -

my_string = "This is a String"
my_string.capitalize --> "This is a string"

Not exactly what we want. Turns out that capitalize converts the *first
character* of the string (letter "T" in this case) to upper case and the
rest of the letter to lower case. In other words, if

my_string1 = "this is a String",
my_string1.capitalize --> "This is a string"

Notice that "t" in "this" became "T" and "S" in "string" became "s".

In order to capitalize the first letter of each word, we have to split the
string into words and then apply capitalize on each word.

my_string.split(" ").each{|word| word.capitalize!}.join(" ") --> "This Is A
String"

We used "capitalize!" instead of "capitalize" . The difference is that
"capitalize!" capitalizes the word in place whereas "capitalize" returns a
copy of the word capitalized.

What if we wanted to capitalize all the letters in the string? Turns out,
this is straight-forward:

my_string.upcase! --> "THIS IS A STRING"


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

 
Old 07-24-2008, 03:51 PM
Xploder HD Movie Player for PS3. Manage, convert and transfer media files between the PC and PS3.
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
I Like To Capitalize The First Letter Of Each Word, Just FYI kevpan815@noreply.com Windows Vista 0 07-12-2008 05:51 PM
Ban Retards who Capitalize the first letter of each word spankydemonkey@gmail.com Windows Vista 4 05-29-2008 05:50 AM
searching old Microsoft word files for text string windsurferLA Windows XP 10 04-01-2008 09:40 AM
How do I capitalize a word when using voice recognition? meuwissenp Microsoft Office 2 12-14-2007 06:00 AM
Find next word to the current word in a string(Leo) Leo Microsoft Office 1 04-09-2007 08:46 AM


All times are GMT -8. The time now is 08:16 PM.


2003 - 2008 All Rights Reserved. Technology Questions

SEO by vBSEO 3.1.0