Convert all first letter to upper case, rest lower for each word

前端 未结 11 2087
一向
一向 2020-12-04 10:15

I have a string of text (about 5-6 words mostly) that I need to convert.

Currently the text looks like:

THIS IS MY TEXT RIGHT NOW

I

11条回答
  •  爱一瞬间的悲伤
    2020-12-04 10:43

    There's a couple of ways to go about converting the first char of a string to upper case.

    The first way is to create a method that simply caps the first char and appends the rest of the string using a substring:

    public string UppercaseFirst(string s)
        {
            return char.ToUpper(s[0]) + s.Substring(1);
        }
    

    The second way (which is slightly faster) is to split the string into a char array and then re-build the string:

    public string UppercaseFirst(string s)
        {
            char[] a = s.ToCharArray();
            a[0] = char.ToUpper(a[0]);
            return new string(a);
        }
    

提交回复
热议问题