;

How to Capitalize the First letter of Each Word in a String in C#


Tutorialsrack 09/02/2020 C# Programs

In this article, we will learn how to capitalize on a first letter of each word in a string in C#. In C#,  there is no built-in function to convert the string into a title case. String class does not have a method to do this. But there are many ways to Capitalize on the first letter of each word in a string. Here is some example to do this:

Method 1: In C#, you can capitalize on the first letter of each word in a string by using a method ToTitleCase() which is present in a TextInfo Class and this class belongs to System.Globalization namespace.

Method 1
using System;
using System.Globalization;

namespace Tutorialsrack
{
    class Program
    {
        /* How to Capitalize a First letter of Each word in a string in C# */
        static void Main(string[] args)
        {
            Console.Write("Enter The String Need to be Capitalized: ");
            
            string str = Console.ReadLine();
            string titleCase = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str.ToLower());            
            //Print The Result
            Console.WriteLine("Capitalized String: "+ titleCase);

            //Hit ENTER to exit the program
            Console.ReadKey();
        }
    }
}
Output

Enter The String Need to be Capitalized: tutorials rack

Capitalized String: Tutorials Rack

 

Enter The String Need to be Capitalized: TUTORIALS RACK

Capitalized String: Tutorials Rack

Method 2: In this method, 

  • Create a character array of the String
  • Run a loop from the first letter to the last letter.
  • Scan through the character, Checking for space.
  • Uppercase the letters following space.
Method 2
using System;

namespace Tutorialsrack
{
    class Program
    {
        /* How to Capitalize a First letter of Each word in a string in C# */
        static void Main(string[] args)
        {
            Console.Write("Enter The String Need to be Capitalized: ");
            string str = Console.ReadLine();
            string titleCase = CapitalizeFirstLetter(str);            
            //Print The Result
            Console.WriteLine("Capitalized String: "+ titleCase);

            //Hit ENTER to exit the program
            Console.ReadKey();
        }

        static string CapitalizeFirstLetter(string value)
        {
            //In Case if the entire string is in UpperCase then convert it into lower
            value = value.ToLower();
            char[] array = value.ToCharArray();
            // Handle the first letter in the string.
            if (array.Length >= 1)
            {
                if (char.IsLower(array[0]))
                {
                    array[0] = char.ToUpper(array[0]);
                }
            }
            // Scan through the letters, checking for spaces.
            // ... Uppercase the lowercase letters following spaces.
            for (int i = 1; i < array.Length; i++)
            {
                if (array[i - 1] == ' ')
                {
                    if (char.IsLower(array[i]))
                    {
                        array[i] = char.ToUpper(array[i]);
                    }
                }
            }
            return new string(array);
        }
    }
}
Output

Enter String to be Capitalized: tutorials rack

Capitalized String: Tutorials Rack

 

Enter String to be Capitalized: TUTORIALS RACK

Capitalized String: Tutorials Rack

I hope this article will help you to understand how to capitalize on a first letter of each word in a string in C#. 

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags