Split a string by , using Regular Expressions in CSharp
Description
The following code shows how to split a string by , using Regular Expressions.
Example
using System;// w ww .j a va2s. c o m
using System.Text.RegularExpressions;
public class RegularExpressions
{
public static void Main()
{
string s = "Oh, I hadn't thought of that";
Regex regex = new Regex(@" |, ");
char[] separators = new char[] {' ', ','};
foreach (string sub in regex.Split(s))
{
Console.WriteLine("Word: {0}", sub);
}
}
}
The code above generates the following result.