language agnostic - Formázza a karakterláncot a cím esetéhez

original title: "language agnostic - Format string to title case"


How do I format a string to title case?

Hogyan formázhatom a karakterláncot a cím esetéhez?

Ez az összefoglalás a fordítás után. Ha meg szeretné tekinteni a teljes fordítást, kattintson a "fordítás" ikonra

Minden válasz
  • Translate

    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);

  • Translate

    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.

  • Translate

    Here's a Perl solution

    Here's a Ruby solution

    Here's a Ruby one-liner solution:

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

  • Translate

    To capatilise it in, say, C - use the ascii codes ( 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.

  • Translate

    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();

  • Translate

    In Perl:

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

    That's even in the FAQ.

  • Translate

    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. Capitalize the first letter of that word 1
    4. Go forward and find the next word
    5. Go to 3 if not at the end of the string, otherwise exit

    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)

  • Translate

    In what language?

    In PHP it is:



    $HelloWorld = ucwords('hello world');

  • Translate

    In Java, you can use the following code.

    public String titleCase(String str) {
        char[] chars = str.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (i == 0) {
                chars[i] = Character.toUpperCase(chars[i]);
            } else if ((i + 1) < chars.length && chars[i] == ' ') {
                chars[i + 1] = Character.toUpperCase(chars[i + 1]);
        return new String(chars);

  • Translate

    Excel-like PROPER:

    public static string ExcelProper(string s) {
        bool upper_needed = true;
        string result = "";
        foreach (char c in s) {
            bool is_letter = Char.IsLetter(c);
            if (is_letter)
                if (upper_needed)
                    result += Char.ToUpper(c);
                    result += Char.ToLower(c);
                result += c;
            upper_needed = !is_letter;
        return result;

  • Translate

    /out/aHR0cDovL3RpdGxlY2FzZS5jb20=/ has an API

  • Translate

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

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

  • Translate

    Here's an implementation in Python:

    And a port of this implementation that I've just done in C++:

  • Translate

    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;

  • Translate

    Here is a simple example of how to do it :

    public static string ToTitleCaseInvariant(string str)
        return System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(str);

  • Translate

    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., '+').

  • Translate

    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()
        // ...
        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);
                    resultado += " " + trozos[i].first;
                resultado += trozos[i].first;
        cout << resultado << endl;
        return 0;

  • Rose Lee

    With perl you could do this:

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