Replace & Split in C#
Regexes as text surgery: split on patterns, replace by function.
using System.Text.RegularExpressions;
var messy = "one,two;three four";
var parts = Regex.Split(messy, @"[,;\s]+");
Console.WriteLine(string.Join("|", parts));
// Replace can compute each substitution from its match
var text = "call 555-0100 or 555-0199";
var masked = Regex.Replace(text, @"\d{3}-\d{4}",
m => new string('*', m.Value.Length));
Console.WriteLine(masked);
How it works
Regex.Splitcuts on any run of separators in one pass.Replaceaccepts a lambda, computing each substitution.- The mask keeps each match's length — data-dependent output.
Keywords and builtins used here
newstringusingvar
The run, in numbers
- Lines
- 11
- Characters to type
- 373
- Tokens
- 73
- Three-star pace
- 85 tpm
At the three-star pace of 85 tokens a minute, this run takes about 52 seconds.
Step 2 of 3 in Regular expressions, step 2 of 17 in The .NET library.