DEV Community

Frederik Van Lierde
Frederik Van Lierde

Posted on

String to CamelCase in dotNet

Camel case is the practice of writing phrases without spaces or punctuation. It indicates the separation of words with a single capitalized letter, and the first word starting with either case. Common examples include "iPhone" and "eBay".

Change to CamelCase

public static string ToCamelCase(this string text)
{
  TextInfo _textInfo = CultureInfo.InvariantCulture.TextInfo;
  char[] _camelCase = _textInfo.ToTitleCase(text).Replace(" ","").ToCharArray();

  _camelCase[0] = char.ToLower(_camelCase[0]);
  return new string(_camelCase);
}
Enter fullscreen mode Exit fullscreen mode

Explanation

To reach camelCase, we do the following

  1. First make it TitleCase
  2. Remove the " " (spaces)
  3. Lowercase the first character

NuGet Package

You can create the extension yourself, or you can download the latest version of CodeHelper.Core.Extensions

This packages contains much more cool and fun string extensions

Top comments (0)