How to Capitalize names

前端 未结 9 1799
余生分开走
余生分开走 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:20

    No, there isn't. Providing you know the string you are handling is a name (or, better to say, a sequence of human names separated by spaces) you should be able to code it yourself within one for cycle and using Char.ToUpper. However, there a culture-specific cases like arabian words "bin", "al" etc. used in names, which shall not be capitalized (providing a latin transcription is used). The same holds for "von" or "van" in western languages.

    Update: Please note that the TextInfo.ToTitleCase serves a different purpose — it's not intended to capitalize first letters of human names, but to provide proper casing of titles (like headlines of news articles to be clear).

    Although the current implementation in .NET can easily serve the requested purpose, I'd avoid doing so. The reason is the implementation may change significantly in the future and hence it's safer to make a custom implementation for human names. Moreover, I doubt the method is really usable for title-casing of strings with respect to the given culture. For example, in Czech ("cs-CZ") the proper title-case should capitalize just the first letter of the first word only.

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

    I use single line:

    string.Join(" ", str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(c => c.Substring(0, 1).ToUpper() + c.Substring(1).ToLower()));
    
    0 讨论(0)
  • 2020-12-15 03:24

    in view

    string titulo = "";
    
    string result = System.Globalization.CultureInfo.TextInfo.ToLower(titulo);
    

    then apply css property

    text-transform = font-family: sans-serif;
    
    0 讨论(0)
提交回复
热议问题