.NET - How can you split a caps delimited string into an array?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In .NET, splitting a string on capital-letter boundaries is a little different from splitting on a literal character such as a comma or slash. The delimiter is not an actual character you want to remove, but a transition point in the text, which makes regular expressions the most practical solution.
Why String.Split Is Not Enough
String.Split works best when you already know the delimiter characters. For a caps-delimited string such as CustomerOrderNumber, the delimiter is really "the position before a new word starts," not a specific symbol.
That means this is a pattern-matching problem, not a plain split problem. The simplest useful tool in .NET is Regex.
A Basic PascalCase Split
If your input is ordinary PascalCase without acronyms, a positive lookahead works well. It splits at positions that are followed by an uppercase letter.
Output:
The empty-string guard is necessary because the first character is uppercase, so the split can produce an empty item at the start.
Handling Acronyms Correctly
The basic split pattern is often too naive for names such as XMLHttpRequest or ParseJSONValue. In those cases, you usually want acronym groups to stay together instead of being split into single letters.
A better approach is to match words instead of splitting between them.
Output:
This pattern covers three useful cases:
- acronym blocks such as
XML, - normal words such as
Request, - numeric groups such as
2or404.
For many real-world identifiers, this produces more intuitive results than splitting purely on uppercase boundaries.
Turning the Result Into a Reusable Helper
If you need this behavior across a project, wrap it in a method instead of repeating the regex inline.
Usage:
Output:
This version is easier to test and adjust if your identifier rules change.
Deciding What "Correct" Means
There is no single perfect answer for every naming convention. You need to decide how you want edge cases handled.
Examples:
- '
MyURLParsermight reasonably becomeMy,URL,Parser.' - '
Version2Updatemight becomeVersion,2,Update.' - '
userIDis camelCase plus an acronym, which may need its own rule if mixed casing matters.'
If your input format is strictly PascalCase, the regex can stay simple. If you expect acronyms, numbers, or mixed casing from many sources, choose a pattern that reflects that reality rather than forcing all strings through a basic split.
Common Pitfalls
- Using
String.Splitdirectly even though there is no literal delimiter character. - Forgetting that a lookahead split can produce an empty first element on PascalCase input.
- Splitting acronyms into single letters when the desired output is
XMLorJSONas one token. - Assuming one regex handles every identifier style without testing real examples from the codebase.
- Recomputing the regex pattern everywhere instead of keeping one shared helper method.
Summary
- Caps-delimited strings are best handled with regular expressions in .NET.
- '
Regex.Splitwith a lookahead is fine for simple PascalCase input.' - '
Regex.Matchesis usually better when you need to preserve acronyms and numbers cleanly.' - Wrap the regex in a helper method if the rule is used more than once.
- Test the pattern against real identifiers, because "correct" splitting depends on your naming conventions.

