Skip to main content

C# extension method that splits a string based on capital letters. e.g. "MyAction" would become "My Action" and "My10Action" would become "My10 Action".

using System.Text.RegularExpressions;

public static class SplitterExtension
{
    private static readonly Regex Reg = new Regex("([a-z,0-9](?=[A-Z])|[A-Z](?=[A-Z][a-z]))", RegexOptions.Compiled);

    /// <summary>
    /// This splits up a string based on capital letters
    /// e.g. "MyAction" would become "My Action" and "My10Action" would become "My10 Action"
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string SplitPascalCase(this string str)
    {
        return Reg.Replace(str, "$1 ");
    }
}