200

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.

3
  • Show us your code that fails. Commented Aug 10, 2009 at 12:28
  • Also, please post what you do get out of it, in addition to what you've posted, what you want to get out of it. Commented Aug 10, 2009 at 12:29
  • 2
    Possible duplicate of Split a string by another string in C# Commented Aug 23, 2019 at 22:58

6 Answers 6

317

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, @"\]\[");
Sign up to request clarification or add additional context in comments.

6 Comments

Or if your minimal code anal like me: 'var parts1 = input.Split(new[] { "][" }, StringSplitOptions.None);'
What's the benefit of providing the StringSplitOptions.None over input.Split(new string[] { "][" })?
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?
@C4u String.Split("[]".ToCharArray()) this would split by both [ and ] separately. For example: Hello[World]Its[Me! would be: Hello, World, Its, Me!
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)
|
57
string tests = "abc][rfd][5][,][.";
string[] reslts = tests.Split(new char[] { ']', '[' }, StringSplitOptions.RemoveEmptyEntries);

5 Comments

Note that this approach assumes he means to split on every ] and [, even when they do not appear in the ][ combination.
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)
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
That's what I was looking for. In the question it was a simple problem but this is an advanced answer.
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.
28

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('-');

4 Comments

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.
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.
In that case, replace with and split on an unlikely character such as ~ or ` or even a non-keyboard character such as ¿ or â—™ etc.
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)))
3
Regex.Split("abc][rfd][5][,][.", @"\]\]");

Comments

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)

Comments

0

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"
*/

3 Comments

How does this differ from the accepted answer given 11 years ago?
Precisely because the answer is very old, whoever sees this question will be able to find more recent and perhaps more functional solutions.
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.