C# String.Split() method isn't behaving itself

edited March 23
I'm trying to test some code with String.Split(), and it's trying to force me to only use a single-character char array, and it won't accept the option System.StringSplitOptions parameter at all.

For instance, this sample code from MSDN fails because "<<" and "..." are multi-character char arrays:
void Main()
{
char[] separatingChars = { "<<", "..." };

string text = "one<<two......three<four";
System.Console.WriteLine("Original text: '{0}'", text);

string[] words = text.Split(separatingChars, System.StringSplitOptions.RemoveEmptyEntries);
System.Console.WriteLine("{0} substrings in text:", words.Length);

foreach (string s in words)
{
System.Console.WriteLine(s);
}
}

Comments

  • edited March 23
    It works just fine after you change char[] in the first line to string[].

  • It's still not working...

    image
  • This works for me:
    void Main()
    {
       string[] separatingChars = { "<<", "..." };
    
       string text = "one<<two......three<four";
       System.Console.WriteLine ("Original text: '{0}'", text);
    
       string[] words = text.Split (separatingChars, System.StringSplitOptions.RemoveEmptyEntries);
       System.Console.WriteLine ("{0} substrings in text:", words.Length);
    
       foreach (string s in words)
       {
          System.Console.WriteLine (s);
       }
    }
  • I got this to work by removing the new string[] portion on the first line.

    Thanks!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!