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.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var