C# Split a String with Multiple Characters as Delimiters

To split a String with multiple characters as delimiters in C#, call Split() on the string instance and pass the delimiter characters array as argument to this method. The method returns a String array with the splits.

Reference to C# String.Split() method.

Example

In the following program, we will take a string ab-cd,efg-hi and split this string with '-' and ',' characters as delimiters using String.Split(Char[]) method.

C# Program

using System;
  
class Example {
    static void Main(string[] args) {
        String str = "ab-cd,efg-hi";
        Char[] delimiters = {',', '-'};
        String[] result = str.Split(delimiters);
        for (int i = 0; i < result.Length; i++) {
            Console.WriteLine(result[i]);
        }
    }
}

Output

ab
cd
efg
hi
ADVERTISEMENT

Conclusion

In this C# Tutorial, we have learned how to split a given string with a multiple character delimiters, using String.Split() method.