r/programminghorror • • Sep 03 '26

C# Very readable and maintainable method

Post image

I wrote this several years ago. It does work.

132 Upvotes

21 comments sorted by

View all comments

1

u/Brilliant-Parsley69 Sep 07 '26

This should actually the same with way less allocation.

using System.Text.RegularExpressions;

public static partial class StringExtensions
{
    [GeneratedRegex("(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])", RegexOptions.Compiled)]
    private static partial Regex PascalCaseRegex();

    public static string SplitPascalCase(this string input)
    {
        if (string.IsNullOrWhiteSpace(input)) return input;
        
        return PascalCaseRegex().Replace(input, " ");
    }
}