Mohanapriya R Mohanapriya R
Updated date Apr 27, 2024
In this article, we will learn how to convert strings into Pascal Case in C#.

What is the Pascal Case?

Pascal Case is a naming convention where each word in a string begins with an uppercase letter, and there are no spaces or punctuation between the words. For example, "convert strings to pascal case" would become "ConvertStringsToPascalCase."

Convert Strings to Pascal Case in C#

To convert a string into Pascal Case in C#, we can use the TextInfo.ToTitleCase method from the TextInfo class, part of the System.Globalization namespace.

using System;
using System.Globalization;

class Program
{
    static void Main()
    {
        string inputString = "convert strings to pascal case";
        string pascalCaseString = ConvertToPascalCase(inputString);

        Console.WriteLine("Original String: " + inputString);
        Console.WriteLine("Pascal Case Output: " + pascalCaseString);
    }

    static string ConvertToPascalCase(string input)
    {
        TextInfo textInfo = CultureInfo.CurrentCulture.TextInfo;
        return textInfo.ToTitleCase(input).Replace(" ", "");
    }
}
  • We begin by defining the input string that we want to convert to Pascal Case.
  • The ConvertToPascalCase method utilizes the TextInfo class to apply title casing to the input string. Additionally, we use Replace it to remove any spaces between words.

Output:

Original String: convert strings to pascal case
Pascal Case Output: ConvertStringsToPascalCase

Comments (0)

There are no comments. Be the first to comment!!!