string.split - by multiple character delimiter

enricco picture enricco · Aug 10, 2009 · Viewed 216.6k times · Source

i am having trouble splitting a string in c# with a delimiter of "][".

For example the string "abc][rfd][5][,][."

Should yield an array containing;
abc
rfd
5
,
.

But I cannot seem to get it to work, even if I try RegEx I cannot get a split on the delimiter.

EDIT: Essentially I wanted to resolve this issue without the need for a Regular Expression. The solution that I accept is;

string Delimiter = "][";  
var Result[] = StringToSplit.Split(new[] { Delimiter }, StringSplitOptions.None);

I am glad to be able to resolve this split question.

Answer

Marc Gravell picture Marc Gravell · Aug 10, 2009

To show both string.Split and Regex usage:

string input = "abc][rfd][5][,][.";
string[] parts1 = input.Split(new string[] { "][" }, StringSplitOptions.None);
string[] parts2 = Regex.Split(input, @"\]\[");