views:

78

answers:

2

I'm trying to split a string:

 string f = r.ReadToEnd();
 string[] seperators = new string[] {"[==========]"};
 string[] result;
result = f.Split(seperators, StringSplitOptions.None);

There's this ========== thing that separates entries. For the life of me, I can't get this to work. I've got a ruby version working...BUT using the string splitter classes I thought I knew for .NET doesn't seem to be working so well.

Any ideas what I'm doing wrong?

+1  A: 

You said that the separator is ========== but you're using [==========]. Try this:

string f = r.ReadToEnd();
string[] seperators = new string[] {"=========="};
string[] result;
result = f.Split(seperators, StringSplitOptions.None);
Claudio Redi
I saw that and thought "no, it couldn't be..."
Daniel Schaffer
Yeah... me too. That would be *too* easy.
Charlie Salts
what can I say? :-) The code looks perfect, that's the only weird thing
Claudio Redi
I got rid of the brackets and it seemed to work. I should have thought of that...the confusing thing is that you are supposed to pass it an array. It should have been clear, though, because the brackets were in the literals.
rsteckly
A: 

When I ran your code with the following modification:

string f = "string1[==========]string2[==========]string3";
string[] seperators = new string[] { "[==========]" };
string[] result;
result = f.Split(seperators, StringSplitOptions.None);
foreach (string x in result) Console.WriteLine(x);

The function writes out the strings as expected. I'd look at the contents of your file more closely - perhaps there is something in the encoding, or some other character that is missing when you devise a separator to work in C#/Windows.

Phil Young