Convert string into Title Case in C# by Extension method Without using TextInfo class



public static class StringExtension
    {
        /// <summary>
        /// Use to convert string in Title Case
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ToTitleCase(this string str)
        {
            if (!String.IsNullOrEmpty(str))
                return String.Join(" ", str.Split(' ').Select(i => i.Length > 0 ? (i.Substring(0, 1).ToUpper() + i.Substring(1).ToLower()) : i).ToArray());

            return str;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            string str = "convert STRING into title case in c#";
            Console.WriteLine(str.ToTitleCase());
            Console.ReadLine();
        }
    }

Output:



Comments

Popular posts from this blog

iframe vs embed vs object in HTML 5

Constructor in c#

What is the need of method overriding ???