In this article, we are going to learn how to convert a string from Title Case to camelCase in C#.
Let’s start.
Initial String Conversion from Title Case to camelCase in C#
To cover all the aspects of a camelCase string, we are going to create the ToCamelCase
method. This method will transform a Title Case string (“Welcome to the Maze”) into a camelCase string (“welcomeToTheMaze”). For greater flexibility, we are going to build it as an extension method.
First of all, a camelCase string cannot have any space (” “) or underscore (“_”) separators.
We have to remove these separators. We can do this by splitting the words and joining them again excluding the separators:
public static string ToCamelCase(this string str) { var words = str.Split(new[] { "_", " " }, StringSplitOptions.RemoveEmptyEntries); return string.Join(string.Empty, words); }
Next, a camelCase string must contain all the joining words as uppercase-first. That means, we have to convert all the lowercase words so that we can get “WelcomeToTheMaze” instead of “WelcometotheMaze”:
public static string ToCamelCase(this string str) { var words = str.Split(new[] { "_", " " }, StringSplitOptions.RemoveEmptyEntries); words = words .Select(word => char.ToUpper(word[0]) + word.Substring(1)) .ToArray(); return string.Join(string.Empty, words); }
Finally, a camelCase string must start with a lowercase letter:
public static string ToCamelCase(this string str) { var words = str.Split(new[] { "_", " " }, StringSplitOptions.RemoveEmptyEntries); var leadWord = words[0].ToLower(); var tailWords = words.Skip(1) .Select(word => char.ToUpper(word[0]) + word.Substring(1)) .ToArray(); return $"{leadWord}{string.Join(string.Empty, tailWords)}"; }
We just turn the leading word to lowercase and keep the remaining words as uppercase-first.
Handling Acronyms
At this point, we have the routine that converts a string from Title Case to camelCase in C#. But, there is still a part missing. We have not handled acronyms yet. For example, the camelCase form of “ISODate” is “isoDate”, not “iSODate”.
This is a tricky part, but we can do this conversion using a regular expression:
public static string ToCamelCase(this string str) { var words = str.Split(new[] { "_", " " }, StringSplitOptions.RemoveEmptyEntries); var leadWord = Regex.Replace(words[0], @"([A-Z])([A-Z]+|[a-z0-9]+)($|[A-Z]\w*)", m => { return m.Groups[1].Value.ToLower() + m.Groups[2].Value.ToLower() + m.Groups[3].Value; }); var tailWords = words.Skip(1) .Select(word => char.ToUpper(word[0]) + word.Substring(1)) .ToArray(); return $"{leadWord}{string.Join(string.Empty, tailWords)}"; }
Now, let’s modify the Program
class to test our method:
var inputs = new[] { "Welcome to the Maze", "Welcome To The Maze", "WelcomeToTheMaze", "Welcome_To_The_Maze", "ISODate", "IOStream" }; foreach (var x in inputs) { Console.WriteLine($"{x} => {x.ToCamelCase()}"); }
Once we start the application, we are going to see our camelCase strings in the output window:
Welcome to the Maze => welcomeToTheMaze Welcome To The Maze => welcomeToTheMaze WelcomeToTheMaze => welcomeToTheMaze Welcome_To_The_Maze => welcomeToTheMaze ISODate => isoDate IOStream => ioStream
Conclusion
In this article, we have learned how to build a helper routine that can convert a string from Title Case to camelCase in C#. We have also learned the special case of acronyms.
Of course, we have to say that this is not a one-size-fits-all solution but an elegant way to achieve camelCase transformation in most cases. That said, we wanted to focus on the basic logic for the transformation without any additional complications by dealing with the encoding system. Due to that, it will not work well for a string with non-ASCII characters.