.NET
string manipulation
split method
array
C#

.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.

csharp
1using System;
2using System.Text.RegularExpressions;
3
4var input = "CustomerOrderNumber";
5var parts = Regex.Split(input, "(?=[A-Z])");
6
7foreach (var part in parts)
8{
9    if (part.Length > 0)
10    {
11        Console.WriteLine(part);
12    }
13}

Output:

text
Customer
Order
Number

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.

csharp
1using System;
2using System.Linq;
3using System.Text.RegularExpressions;
4
5var input = "XMLHttpRequest";
6
7var parts = Regex.Matches(
8    input,
9    "[A-Z]+(?![a-z])|[A-Z]?[a-z]+|\\d+"
10)
11.Select(match => match.Value)
12.ToArray();
13
14Console.WriteLine(string.Join(", ", parts));

Output:

text
XML, Http, Request

This pattern covers three useful cases:

  • acronym blocks such as XML,
  • normal words such as Request,
  • numeric groups such as 2 or 404.

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.

csharp
1using System.Linq;
2using System.Text.RegularExpressions;
3
4public static class WordSplitter
5{
6    private static readonly Regex WordPattern =
7        new Regex("[A-Z]+(?![a-z])|[A-Z]?[a-z]+|\\d+");
8
9    public static string[] SplitCapsDelimited(string input)
10    {
11        return WordPattern.Matches(input)
12            .Select(match => match.Value)
13            .ToArray();
14    }
15}

Usage:

csharp
1using System;
2
3var words = WordSplitter.SplitCapsDelimited("ParseJSONValue2");
4Console.WriteLine(string.Join(" | ", words));

Output:

text
Parse | JSON | Value | 2

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:

  • 'MyURLParser might reasonably become My, URL, Parser.'
  • 'Version2Update might become Version, 2, Update.'
  • 'userID is 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.Split directly 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 XML or JSON as 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.Split with a lookahead is fine for simple PascalCase input.'
  • 'Regex.Matches is 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.

Course illustration
Course illustration

All Rights Reserved.