Separate Title Cases - CSharp System

CSharp examples for System:String Case

Description

Separate Title Cases

Demo Code


using System.Text;

public class Main{
        public static string SeparateTitleCases(this string source, string separator = " ")
        {/*  ww  w . j  av a  2  s  .  c o  m*/
            var result = new StringBuilder();
            char previousChar = default(char);

            for (int i = 0; i < source.Length; i++)
            {
                char currentChar = source[i];

                if (char.IsLower(previousChar) && // Previous char is lowercase
                    char.IsUpper(currentChar)) // Current char is uppercase
                {
                    result.Append(separator); // Append separator
                }
                result.Append(currentChar);

                previousChar = currentChar;
            }

            return result.ToString();
        }
}

Related Tutorials