Split a string with delimiters but keep the delimiters in the result in C#

If the split chars were ,, ., and ;, I’d try:

using System.Text.RegularExpressions;
...    
string[] parts = Regex.Split(originalString, @"(?<=[.,;])")

(?<=PATTERN) is positive look-behind for PATTERN. It should match at any place where the preceding text fits PATTERN so there should be a match (and a split) after each occurrence of any of the characters.

Leave a Comment