C# String Extension to Split Camel Case C# String Extension to Split Camel Case

Let me show you a quick method to convert Camel Case in C# to title case.


How to Convert String from Title Case to camelCase in C#

I'm a big fan of extension methods as I have previously blogged about when it comes to C# Ordinal String Extension or C# Truncate String Extension to name a few examples.

This next extension is a nice little handy one when you want to convert a string that is CamelCased to a more readable string like Camel Cased.

Initial String Conversion from Title Case to camelCase in C#

I most often use this function when I have an Enum and I want to get a more human readable version without much effort using a regular expression.

Let's take a look at the code and it's usage and if you want more extension methods I have one to help convert date with C#, enough of that though and let's look at splitcamelcase:

using System;
using System.Linq;
using System.Text.RegularExpressions;
namespace Common.Extensions
{
public static class StringExtensions
{
public static string SplitCamelCase(this string str)
{
return Regex.Replace(
Regex.Replace(
str,
@"(\P{Ll})(\P{Ll}\p{Ll})",
"$1 $2"
),
@"(\p{Ll})(\P{Ll})",
"$1 $2"
);
}
}
}

C# Split Camelcase

This can then be used as follows which is quite similar to how the final implementation of JavaScript String replace() works:

var myString = "ThisIsCamelCased";
Console.WriteLine(myString.SplitCamelCase());

This will output the string: This Is Camel Cased.

Frequently Asked Questions

What is camel case in C#?

In camel cases there is a combination of two words without space or underscores.

What is an example of CamelCase?

What does camel case mean in the English language? The use of capital letters to begin the second word in a pronounced compound name.

Example of camel cases include "IPods" and "GaGa". How can you find out?

How do you make a CamelCase string in C#?

In order to cover all aspects of the camelcase string, we'll introduce the toCamelCaser method. The method will convert the titlecase string ("Welcome To The Maze") to the camelcase string ("Welcome To The Maze").

In order to be flexible, we are using the extension system. Unlike the camel case strings there can be no separate spaces ("" ") or underscores ( " " " _ " separators. It's gonna need some sort of removal.

The second step would include combining the two words together, excluding the separators.

What is CamelCase vs PascalCase?

Camel case is reminiscent of Pascal. Both demand variables are made of compound words, and each word's initial letters are enclosed by upper case letters.

It differs from the case of Pascal, in which the first word has an upper case letter, whereas camele doesn't.

What is CamelCase in C#?

In a camel casing the first letter of one word is lowercase and the first letter the following is capitalized.

What is CamelCase example?

Meaning of camel in english. In compound names, use of capital letters to start the second word if there is no difference between them. examples of camel case include the words iPod/GaGa.

How do you convert a string to a proper case?

For converting text strings in titlecases, the String Titles method can use the String Titles() method. Title() returns the name in titlecases.

The title() method changes the initial character to uppercase and the rest to uppercase.

Published on Jun 9, 2022

Tags: ASP.NET MVC and Web API Tutorial | c# | extension

Related Posts

Did you enjoy this article? If you did here are some more articles that I thought you will enjoy as they are very similar to the article that you just finished reading.

Tutorials

Learn how to code in HTML, CSS, JavaScript, Python, Ruby, PHP, Java, C#, SQL, and more.

No matter the programming language you're looking to learn, I've hopefully compiled an incredible set of tutorials for you to learn; whether you are beginner or an expert, there is something for everyone to learn. Each topic I go in-depth and provide many examples throughout. I can't wait for you to dig in and improve your skillset with any of the tutorials below.