Regularly we use string.Split function with single character delimiter. But if you want to split string by multiple characters delimiter use the following code.Program:using System;
using System.Text.RegularExpressions;
namespace DotNetMirror
{
class StringSplitByMultiCharacter
{
static void Main(string[] args)
{
string str = "A,B,C,D,E";
//split by single char
string[] splitBySingleChar = str.Split(','); //output: A B C D E
string strMultiChars = "A{,}B,{,}C{,}D,{,}E";
//split by string array
string[] splitByMultipleSring = strMultiChars.Split(new string[] { "{,}" }, StringSplitOptions.None);
foreach (string s in splitByMultipleSring)
{
Console.WriteLine(s);
}
//alternative - split by regex
string[] splitByMultipleCharRegEx = Regex.Split(strMultiChars, "{,}");
Console.ReadKey();
}
}
} Output:
|