How to Capitalize names

前端 未结 9 1798
余生分开走
余生分开走 2020-12-15 02:46

so basically if i want to transform a name from

stephen smith 

to

Stephen Smith

i can easily do it with c

相关标签:
9条回答
  • 2020-12-15 03:08

    This works for me with surnames that have a ' character in them.

            if (Surname.Contains("'"))
            {
               String[] Names = Surname.Split('\'').ToArray();
               Surname = textInfo.ToTitleCase(Names[0].ToString());
               Surname += "''";
               Surname += textInfo.ToTitleCase(Names[1].ToString());
            }
    
    0 讨论(0)
  • 2020-12-15 03:09

    A slight extension on the answer offered by Pedro:

    Regex.Replace(Name, @"(?:(M|m)(c)|(\b))([a-z])", delegate(Match m) { 
        return String.Concat(m.Groups[1].Value.ToUpper(), m.Groups[2].Value, m.Groups[3].Value, m.Groups[4].Value.ToUpper());
     });
    

    This will correctly capitalize McNames in addition to title case. eg "simon mcguinnis" --> "Simon McGuinnis"

    • The first non-capture group will match any word-break character OR "Mc" / "mc".
    • If it matches a word-break, then groups 1 and 2 are empty and group 3 contains that character.
    • If it matches "Mc" or "mc" the groups 1 and 2 contain "m" and "c" and group 3 is empty.

      • Group 1 (the "m" or "M") is capitalized.
      • Group 2 (the "c") remains un-altered.
      • Group 3 (the break character) remains un-altered.
      • Group 4 (the first letter of the next word) is capitalized.

    All 4 groups, empty or otherwise, are concatenated to generate the return string.

    0 讨论(0)
  • 2020-12-15 03:10

    This is an extension method on the string class that capitalizes a single word. You can use it alongside a str.Split() and str.Join to capitalize every word of the str string. You can add checks for empty or one character length strings.

    public static string Capitalize(this string word)
    {
        return word.Substring(0, 1).ToUpper() + word.Substring(1).ToLower();
    }
    
    0 讨论(0)
  • 2020-12-15 03:11

    Hope this helps :)... But note that the process will most likely be slow if you have many, many strings to change case...

        string str = "to title case";
        Char[] ca = str.ToCharArray();
    
        foreach(Match m in Regex.Matches(str, @"\b[a-z]"))
        {
            ca[m.Index] = Char.ToUpper(ca[m.Index]);
        }
        Console.WriteLine(new string(ca));
    

    Update: Or you could also use a custom evaluator to change the case like this:

        string str = "to title case";
        Console.WriteLine(Regex.Replace(str, @"\b[a-z]", delegate (Match m) 
                                                      {
                                                          return m.Value.ToUpper();
                                                      }
                          ));
    

    Note that in my test with 1,000,000 iterations the first method was only 0.48 seconds faster than the one with the evaluator (The first one took 6.88 seconds and the latter 7.36 seconds to complete the 1,000,000 iterations) so I wouldn't take speed into account to choose either...

    0 讨论(0)
  • 2020-12-15 03:17

    You can do this using the ToTitleCase method of the System.Globalization.TextInfo class:

    CultureInfo cultureInfo   = Thread.CurrentThread.CurrentCulture;
    TextInfo textInfo = cultureInfo.TextInfo;
    
    Console.WriteLine(textInfo.ToTitleCase(title));
    Console.WriteLine(textInfo.ToLower(title));
    Console.WriteLine(textInfo.ToUpper(title));
    
    0 讨论(0)
  • 2020-12-15 03:18

    Names are tricky. The simple rules of First Letters do not apply. The only sensible approach here is to ask your users how they want it. Anything else can cause offence.

    If my name is MacPhearson, ODowel, or just simply marc, Marc or even mArC - then frankly: leave it alone. Trust the user to get it right. This gets even more tricky as you go between cultures.

    0 讨论(0)
提交回复
热议问题