0

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?

2
  • What output do you get? It's possible the code is doing what you're asking it to do, but is not doing what you want it to do. Commented May 3, 2010 at 2:38
  • 2
    Examples of the input and output would be helpful. Commented May 3, 2010 at 2:39

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

4 Comments

I saw that and thought "no, it couldn't be..."
Yeah... me too. That would be too easy.
what can I say? :-) The code looks perfect, that's the only weird thing
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.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.