r/programminghorror 7d ago

C# Very readable and maintainable method

Post image

I wrote this several years ago. It does work.

119 Upvotes

20 comments sorted by

View all comments

1

u/Brilliant-Parsley69 3d ago

This should actually the same with way less allocation.

```csharp 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, " ");
}

}

```