Home » Questions » Computers [ Ask a new question ]

Format string to title case

Format string to title case

How do I format a string to title case?

Asked by: Guest | Views: 29
Total answers/comments: 3
Guest [Entry]

"I would be wary of automatically upcasing all whitespace-preceded-words in scenarios where I would run the risk of attracting the fury of nitpickers.

I would at least consider implementing a dictionary for exception cases like articles and conjunctions. Behold:

""Beauty and the Beast""

And when it comes to proper nouns, the thing gets much uglier."
Guest [Entry]

"Here's a Perl solution http://daringfireball.net/2008/05/title_case

Here's a Ruby solution http://frankschmitt.org/projects/title-case

Here's a Ruby one-liner solution: http://snippets.dzone.com/posts/show/4702

'some string here'.gsub(/\b\w/){$&.upcase}

What the one-liner is doing is using a regular expression substitution of the first character of each word with the uppercase version of it."
Guest [Entry]

"To capatilise it in, say, C - use the ascii codes (http://www.asciitable.com/) to find the integer value of the char and subtract 32 from it.

This is a poor solution if you ever plan to accept characters beyond a-z and A-Z.

For instance: ASCII 134: å, ASCII 143: Å.
Using arithmetic gets you: ASCII 102: f

Use library calls, don't assume you can use integer arithmetic on your characters to get back something useful. Unicode is tricky."