Remove any underscores or dashes and convert a string into camel casing. - CSharp System

CSharp examples for System:String Convert

Description

Remove any underscores or dashes and convert a string into camel casing.

Demo Code


using System.Text.RegularExpressions;
using System.Text;
using System.Globalization;
using System;/*from  www  . jav a2 s.c  o m*/

public class Main{
        /// <summary>
        /// Remove any underscores or dashes and convert a string into camel casing.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        /// <example>StringHelper.Camelize("data_rate"); //"dataRate"</example>
        /// <example>StringHelper.Camelize("background-color"); //"backgroundColor"</example>
        /// <example>StringHelper.Camelize("-webkit-something"); //"WebkitSomething"</example>
        /// <example>StringHelper.Camelize("_car_speed"); //"CarSpeed"</example>
        /// <example>StringHelper.Camelize("yes_we_can"); //"yesWeCan"</example>
        // Inspired by http://stringjs.com/#methods/camelize
        public static string Camelize(string value)
        {
            if (String.IsNullOrWhiteSpace(value)) { return value; }
            return Regex.Replace(value, @"[-_]\p{L}", m => m.ToString().ToUpper(CultureInfo.CurrentCulture)).Replace("-", "").Replace("_", "");
        }
}

Related Tutorials