How to capitalize the first letter of each word in a string

By Alan S. at July 23, 2010 12:43
Filed Under: Web / Software Development

This is going to be the shortest post on this board, and one of the most embarrassing!

 

I ‘inherited’ a file full of string values that contained over 10,000 entries. Problem was, they were all lower case. I wasn’t about to sit there and manually capitalize the first letter of each word in the list so I thought, “Hey… just write a quick function in the program that does it for you. It should only take a few lines, right?” Well, not for me.

 

I started working on a function that read in each line, searched for the first character and capitalized it. But how would you account for some words that should not be capitalized like ‘de’ as in “Cul de Sac” or initials or abbreviations? The more I thought about it, the more complex the function got. I started worrying that I was missing something… Well, I was.

 

Like I said, it’s kind of embarrassing, but while Googling to try and make sure I covered all my bases, I came across this gem on the MSDN site:

 

myString = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase( myString );

 

I couldn't believe it. It takes care of all automatic capitalization of the first letter of a word or string of words... Ugh! Oh well, I hope at least it saves some developer an hour of headaches!

Bookmark and Share DotnetKicks dotnetshoutout

Comments

7/23/2010 12:44:48 PM #

trackback

How to capitalize the first letter of each word in a string

You've been kicked (a good thing) - Trackback from DotNetKicks.com

DotNetKicks.com | Reply

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading




   

eMail Scraper
Generate email lists in seconds!


eSource Development presents the ultimate tool for email lead generation! They have decided to release the hottest email list generation tool that allows you to get hundreds (even THOUSANDS) of specific email addresses for any genre, niche, or geographical area.

Internet marketing companies and professionals have been using this tool for years. Now, it has been re-engineered, updated, and released to the public. This 'insider only' software was a closely guarded industry secret until recently.



Watch the Demo

NEW! Trial version available!

DOWNLOAD FOR FREE




  


Dr. Torgo's PC
System Inventory v2.0


Dr. Torgo's PC System Inventory offers a full range of system query options and powerful reporting tools. This software quickly generates reports on several dozen hives of system information including disks, CPU, memory, motherboard, users, ports, services, software, and MORE.

Read more here!

NEW! Trial version available!
DOWNLOAD FOR FREE


Help us out by visiting our sponsors!

Rifftrax







Recent Comments

Comment RSS

What We're Playing





Who's Watchin' Me?