Converts a string to a camel case representation, e.g. thisIsCamelCasing with an initial lower case character. - CSharp System

CSharp examples for System:String Convert

Description

Converts a string to a camel case representation, e.g. thisIsCamelCasing with an initial lower case character.

Demo Code

// Copyright (c) Microsoft Corporation. All rights reserved.
using System.Text;
using System.Globalization;
using System;/*from w  ww  . j  a v  a2  s  .  com*/

public class Main{
        /// <summary>
        /// Converts a string to a camel case representation, e.g. <c>thisIsCamelCasing</c> with
        /// an initial lower case character.
        /// </summary>
        /// <param name="value">The string to convert.</param>
        /// <returns>The camel cased string; or the original string if no modifications were necessary.</returns>
        public static string ToCamelCase(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return value;
            }

            if (char.IsLower(value[0]))
            {
                return value;
            }

            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < value.Length; i++)
            {
                bool flag = i + 1 < value.Length;
                if (i != 0 && flag && char.IsLower(value[i + 1]))
                {
                    stringBuilder.Append(value.Substring(i));
                    break;
                }

                char lower = char.ToLower(value[i], CultureInfo.InvariantCulture);
                stringBuilder.Append(lower);
            }

            return stringBuilder.ToString();
        }
}

Related Tutorials