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, "you are welcome to tehiehook.com" would become "YouAreWelcomeToTehiehook.Com."
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 = "you are welcome to tehiehook.com";
string pascalCaseString = ConvertToPascalCase(inputString);
Console.WriteLine("Original String: " + inputString);
Console.WriteLine("Pascal Case Output: " + pascalCaseString);
Console.ReadKey();
}
static string ConvertToPascalCase(string input)
{
TextInfo textInfo = CultureInfo.CurrentCulture.TextInfo;
return textInfo.ToTitleCase(input).Replace(" ", "");
}
}
We assigned the input string that we wanted to convert to Pascal Case. The ConvertToPascalCase
method used the TextInfo
class to apply title casing to the input string. Additionally, we have used the Replace
it to remove any spaces between words.
Output:
Original String: you are welcome to tehiehook.com
Pascal Case Output: YouAreWelcomeToTehiehook.Com
Comments (0)