views:

2282

answers:

16

How do I format a string to title case?

+9  A: 

Here is a simple static method to do this in C#:

public static string ToTitleCaseInvariant(string targetString)
{
return System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(targetString);
}
tags2k
Not available in silverlight. See other C# answer(s)
Simon_Weaver
If that isn't the longest qualification ever... `Throw.In.Todays.News.While.Youre.At.It(...);`
Thanatos
+2  A: 

In what language?

In PHP it is:

ucwords()

example:

$HelloWorld = ucwords('hello world');
cmcculloh
A: 

Without using a ready-made function, a super-simple low-level algorithm to convert a string to title case:

convert first character to uppercase.
for each character in string,
    if the previous character is whitespace,
        convert character to uppercase.

This asssumes the "convert character to uppercase" will do that correctly regardless of whether or not the character is case-sensitive (e.g., '+').

Rudd Zwolinski
This doesn't do title case, where small words (articles and prepositions) should not be capitalized.
brian d foy
+3  A: 

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) Read in the string
(2) Take the first word
(3) Capatilise the first letter of that word*
(4) Go forward and find the next word
(5) Go to 3 if not at the end of the string, otherwise exit

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

There would need to be much more error checking in the code (ensuring valid letters etc.), and the "Capatilise" 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 http://answers.google.com/answers/threadview?id=349913 is a good scheme)

Lehane
Totally doesn't work on code tables like e.g. German where the umlauts (äöü) have upper case letters that don't follow that pattern.
xmjx
+4  A: 

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.

engtech
+7  A: 

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.

Ishmaeel
+3  A: 
Bill
A: 

With perl you could do this:

my $tc_string = join ' ', map { ucfirst($_) } split /\s+/, $string;

robbiebow
This doesn't do title case, where small words (articles and prepositions) should not be capitalized.
brian d foy
A: 

See http://kiev.pm.org/node/229

+2  A: 

In Perl:

$string =~ s/(\w+)/\u\L$1/g;

That's even in the FAQ.

Randal Schwartz
It's not in the FAQ anymore because it's wrong. Think about what that does to contractions :)
brian d foy
A: 

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.

#include <iostream>
#include <string>
#include <vector>
#include <cctype>
#include <set>

using namespace std;

typedef vector<pair<string, int> > subDivision;
set<string> nonUpperCaseAble;

subDivision split(string & cadena, string delim = " "){
    subDivision retorno;
    int pos, inic = 0;
    while((pos = cadena.find_first_of(delim, inic)) != cadena.npos){
     if(pos-inic > 0){
      retorno.push_back(make_pair(cadena.substr(inic, pos-inic), inic));
     }
     inic = pos+1;
    }
    if(inic != cadena.length()){
     retorno.push_back(make_pair(cadena.substr(inic, cadena.length() - inic), inic));
    }
    return retorno;
}

string firstUpper (string & pal){
    pal[0] = toupper(pal[0]);
    return pal;
}

int main()
{
    nonUpperCaseAble.insert("the");
    nonUpperCaseAble.insert("of");
    nonUpperCaseAble.insert("in");
    // ...

    string linea, resultado;
    cout << "Type the line you want to convert: " << endl;
    getline(cin, linea);

    subDivision trozos = split(linea);
    for(int i = 0; i < trozos.size(); i++){
     if(trozos[i].second == 0)
     {
      resultado += firstUpper(trozos[i].first);
     }
     else if (linea[trozos[i].second-1] == ' ')
     {
      if(nonUpperCaseAble.find(trozos[i].first) == nonUpperCaseAble.end())
      {
       resultado += " " + firstUpper(trozos[i].first);
      }else{
       resultado += " " + trozos[i].first;
      }
     }
     else
     {
      resultado += trozos[i].first;
     }  
    }

    cout << resultado << endl;
    getchar();
    return 0;
}
A: 

Any suggestion about Javascript?

codebreak
A: 

I think using the CultureInfo is not always reliable, this the the simple and handy way to manipulate string manually:

string sourceName = txtTextBox.Text.ToLower(); string destinationName = sourceName[0].ToUpper();

for (int i = 0; i < (sourceName.Length - 1); i++) { if (sourceName[i + 1] == "") { destinationName += sourceName[i + 1]; } else { destinationName += sourceName[i + 1]; } txtTextBox.Text = desinationName;

A: 

There is a built-in formula PROPER(n) in Excel.

Was quite pleased to see I didn't have to write it myself!

Simon_Weaver
A: 

http://titlecase.com/ has an API

Simon_Weaver
A: 

In Silverlight there is no ToTitleCase in the TextInfo 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.

    public string TitleCase(string str)
    {
        return Regex.Replace(str, @"\w+", (m) =>
        {
            string tmp = m.Value;
            return char.ToUpper(tmp[0]) + tmp.Substring(1, tmp.Length - 1).ToLower();
        });
    }
Simon_Weaver