How do I format a string to title case?
original title: "language agnostic - Format string to title case"
How do I format a string to title case?
मैं केस दर्ज करने के लिए एक स्ट्रिंग कैसे प्रारूपित करूं?
यह अनुवाद के बाद का सारांश है, अगर आपको पूरा अनुवाद देखने की आवश्यकता है, तो कृपया 'अनुवाद' आइकन पर क्लिक करें
Here is a simple static method to do this in C#:
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:
And when it comes to proper nouns, the thing gets much uglier.
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
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.
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.
In Silverlight there is no
ToTitleCase
in theTextInfo
class.Here's a simple regex based way.
Note: Silverlight doesn't have precompiled regexes, but for me this performance loss is not an issue.
In Perl:
That's even in the FAQ.
If the language you are using has a supported method/function then just use that (as in the C#
ToTitleCase
method)If it does not, then you will want to do something like the following:
1 To capitalize it in, say, C - use the ascii codes to find the integer value of the char and subtract 32 from it.
There would need to be much more error checking in the code (ensuring valid letters etc.), and the "Capitalize" function will need to impose some sort of "title-case scheme" on the letters to check for words that do not need to be capatilised ('and', 'but' etc. Here is a good scheme)
In what language?
In PHP it is:
ucwords()
example:
In Java, you can use the following code.
Excel-like PROPER:
/out/aHR0cDovL3RpdGxlY2FzZS5jb20=/
has an APIThere is a built-in formula
PROPER(n)
in Excel.Was quite pleased to see I didn't have to write it myself!
Here's an implementation in Python: https://launchpad.net/titlecase.py
And a port of this implementation that I've just done in C++: http://codepad.org/RrfcsZzO
I think using the CultureInfo is not always reliable, this the the simple and handy way to manipulate string manually:
Here is a simple example of how to do it :
Without using a ready-made function, a super-simple low-level algorithm to convert a string to title case:
This asssumes the "convert character to uppercase" will do that correctly regardless of whether or not the character is case-sensitive (e.g., '+').
Here you have a C++ version. It's got a set of non uppercaseable words like prononuns and prepositions. However, I would not recommend automating this process if you are to deal with important texts.
With perl you could do this: