186

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.

6 Answers6

308

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, @"\]\[");
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 8
    Or if your minimal code anal like me: 'var parts1 = input.Split(new[] { "][" }, StringSplitOptions.None);' – Paul Grimshaw Jul 10 '14 at 23:41
  • 3
    What's the benefit of providing the `StringSplitOptions.None` over `input.Split(new string[] { "][" })`? – seebiscuit Jan 25 '16 at 19:09
  • 1
    This presupposes, that there are both characters right? What if I want to split by either "[" or "]"? From my tests so far I guess thats a different story, right? – C4d Mar 07 '16 at 15:28
  • 4
    @C4u `String.Split("[]".ToCharArray())` this would split by both [ and ] separately. For example: Hello[World]Its[Me! would be: Hello, World, Its, Me! – Ma Dude Jul 19 '18 at 08:06
  • 1
    It seems that ", StringSplitOptions.None" is needed otherwise it will try to use the method which takes as parameter a char instead (but I am very surprised by this behavior) – azerty Oct 22 '18 at 15:04
54
string tests = "abc][rfd][5][,][.";
string[] reslts = tests.Split(new char[] { ']', '[' }, StringSplitOptions.RemoveEmptyEntries);
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
  • 9
    Note that this approach assumes he means to split on every ] and [, even when they do not appear in the ][ combination. – Lasse V. Karlsen Aug 10 '09 at 12:34
  • 2
    That overload of string.Split is treating it as 2 separate delimiters - simply you are discarding the rest. There is an overload that accepts a string as a delimiter (actually, an array of strings) - so you don't have to remove the empty values (meaning also: you can **find** empty values when they validly exist in the data) – Marc Gravell Aug 10 '09 at 12:34
  • Your ] assumption is [ correct. He didnt specify, so I gave him something that would work for his example. If he doesnt want to skip them, then would work. But without more information, we'll never know – SwDevMan81 Aug 10 '09 at 12:45
  • That's what **I** was looking for. In the question it was a simple problem but this is an advanced answer. – Bitterblue Mar 07 '14 at 09:12
  • That was exactly what I was looking for. But the correct answer is also news to me, didn't know you could split with more than one character as Marc described. The syntax is pretty rubbish. – Piotr Kula Jun 09 '15 at 12:43
26

Another option:

Replace the string delimiter with a single character, then split on that character.

string input = "abc][rfd][5][,][.";
string[] parts1 = input.Replace("][","-").Split('-');
seabass2020
  • 1,093
  • 14
  • 12
  • 1
    This is what I do. It gives me a normalized, general method that works on all platforms without having to worry about platform-specific tricks. – David Betz Jan 02 '16 at 22:38
  • 7
    Not a good idea. Consider: `string input = "abc][rf-d][5"`. The "-" in "rf-d" will be caught as a delimiter. See the accepted answer above instead. – Gilad Barner Jan 06 '16 at 23:31
  • In that case, replace with and split on an unlikely character such as ~ or ` or even a non-keyboard character such as ¿ or ◙ etc. – seabass2020 Jan 11 '16 at 20:35
  • 1
    I like the solution in case its clear, robust and can become normalized. My usage `Dim Levels As New List(Of String)(newSelectedTask.Replace(LevelDelimter, Chr(13)).Split(Chr(13)))` – Nasenbaer Jun 26 '20 at 22:10
3
Regex.Split("abc][rfd][5][,][.", @"\]\]");
Christopher Klewes
  • 11,181
  • 18
  • 74
  • 102
1

More fast way using directly a no-string array but a string:

string[] StringSplit(string StringToSplit, string Delimitator)
{
    return StringToSplit.Split(new[] { Delimitator }, StringSplitOptions.None);
}

StringSplit("E' una bella giornata oggi", "giornata");
/* Output
[0] "E' una bella giornata"
[1] " oggi"
*/
Marco Concas
  • 1,665
  • 20
  • 25
  • 1
    How does this differ from [the accepted answer given 11 years ago](https://stackoverflow.com/a/1254596/215552)? – Heretic Monkey Dec 03 '20 at 17:09
  • Precisely because the answer is very old, whoever sees this question will be able to find more recent and perhaps more functional solutions. – Marco Concas Dec 04 '20 at 15:33
  • 4
    The "very old" answer uses the same code as your more recent and more functional solution. In any case, my comment was meant to prompt you to edit your answer with more explanation as to how your code is better or different from existing answers. – Heretic Monkey Dec 04 '20 at 15:42
1

In .NETCore 2.0 and beyond, there is a Split overload that allows this:

string delimiter = "][";
var results = stringToSplit.Split(delimiter);

Split (netcore 2.0 version)

Bob2Chiv
  • 1,858
  • 2
  • 19
  • 29