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

前端 未结 11 2066
一向
一向 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条回答
  •  -上瘾入骨i
    2020-12-04 10:29

    jspcal's answer as a string extension.

    Program.cs

    class Program
    {
        static void Main(string[] args)
        {
            var myText = "MYTEXT";
            Console.WriteLine(myText.ToTitleCase()); //Mytext
        }
    }
    

    StringExtensions.cs

    using System;
    public static class StringExtensions
    {
    
        public static string ToTitleCase(this string str)
        {
            if (str == null)
                return null;
    
            return System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(str.ToLower());
        }
    }
    

提交回复
热议问题