How to Capitalize names

前端 未结 9 1800
余生分开走
余生分开走 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: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();
    }
    

提交回复
热议问题