Mega Code Archive

 
Categories / C# Book / 11 Regular Expression Basics
 

0662 Splitting Text

Regex.Split method is more powerful version of string.Split method. In this example, we split a string, where any digit counts as a separator: using System; using System.Text.RegularExpressions; class Program { static void Main(string[] args) { foreach (string s in Regex.Split ("a1b1c", @"\d")) Console.Write (s + " "); } } The output: a b c The following splits a camel-case string into separate words: using System; using System.Text.RegularExpressions; class Program { static void Main(string[] args) { foreach (string s in Regex.Split ("oneTwoThree", @"(?=[A-Z])")) Console.Write (s + " "); } } The output: one Two Three