DEV Community

Frederik Van Lierde
Frederik Van Lierde

Posted on

1

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

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay