Returns a converted camel cased string into a string delimited by dashes. - CSharp System

CSharp examples for System:String Convert

Description

Returns a converted camel cased string into a string delimited by dashes.

Demo Code


using System.Text.RegularExpressions;
using System.Text;
using System.Globalization;
using System;//from  w  ww .  java 2s  .  c  o  m

public class Main{
        /// <summary>
        /// Returns a converted camel cased string into a string delimited by dashes.
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        /// <example>StringHelper.Dasherize("dataRate"); //"data-rate"</example>
        /// <example>StringHelper.Dasherize("CarSpeed"); //"-car-speed"</example>
        /// <example>StringHelper.Dasherize("YesWeCan"); //"yes-we-can"</example>
        // Inspired by http://stringjs.com/#methods/dasherize
        public static string Dasherize(string value)
        {
            if (String.IsNullOrWhiteSpace(value)) { return value; }
            return Regex.Replace(value, @"\p{Lu}", m => "-" + m.ToString().ToLower(CultureInfo.CurrentCulture));
        }
}

Related Tutorials