3

I know that System.String.Split(null) should return me a string array with whitespace removed. I've read this post and this MSDN doc, which does not agree with what I'm experiencing.

Here's my code:

void MyFunction(string info)
{
    print(info);
    print(Char.IsWhiteSpace(info,0));
    print(Char.IsWhiteSpace(info,1));
    print(Char.IsWhiteSpace(info,2));
    print(Char.IsWhiteSpace(info,3));
    print(Char.IsWhiteSpace(info,4));
    print(Char.IsWhiteSpace(info,5));
    print(Char.IsWhiteSpace(info,6));
    print(Char.IsWhiteSpace(info,7));
    print(Char.IsWhiteSpace(info,8));
    print(Char.IsWhiteSpace(info,9));
    print(Char.IsWhiteSpace(info,10));
    print(Char.IsWhiteSpace(info,11));

    string [] split = info.Split();
    foreach(string s in split)
        print(s);
}

Here's the output:

628      5911.3097      1660.0134      3771.8285              0
False
False
False
True
True
True
True
True
True
False
False
False
628
(empty)
(empty)
(empty)
(empty)
(empty)
5911.3097
(empty)
(empty)
(empty)
(empty)
(empty)
1660.0134
(empty)
(empty)
(empty)
(empty)
(empty)
3771.8285

It seems to me that System.String.Split(null) just removed one space for me :S

I'm using: Unity3D, Mono, C#, Mac OSX 10.8

Community
  • 1
  • 1
CherryQu
  • 3,343
  • 9
  • 40
  • 65
  • Split Returns a string array that contains the substrings in this instance that are delimited by elements.So here it return substring...with each space it found...it doesnt remove spaces... – Amit Singh Jun 06 '13 at 05:31
  • 1
    If two delimiters are adjacent, or a delimiter is found at the beginning or end of this instance, the corresponding array element contains Empty.so its not space it empty.... – Amit Singh Jun 06 '13 at 05:35
  • @AmitSingh Thanks! I've edited the output in my question. – CherryQu Jun 06 '13 at 06:25

1 Answers1

6

I suspect the problem is you're confusing an empty string with a space. Let me demonstrate:

    static void Main(string[] args)
    {
        var info = "628      5911.3097      1660.0134      3771.8285              0";
        Console.WriteLine(info);
        //foreach (var c in info)
        //    Console.WriteLine(Char.IsWhiteSpace(c));

        Console.WriteLine();

        string[] split = info.Split();
        foreach (string s in split)
            Console.WriteLine("\"" + s + "\" is empty: " + (s.Length == 0));

        //What happens if we concat the strings?
        Console.WriteLine();
        Console.WriteLine(string.Concat(split));

        Console.ReadLine();

        /*
        628      5911.3097      1660.0134      3771.8285              0

        "628" is empty: False
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "5911.3097" is empty: False
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "1660.0134" is empty: False
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "3771.8285" is empty: False
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "" is empty: True
        "0" is empty: False

        6285911.30971660.01343771.82850
        */
    }

In future may I suggest you using the following API call?

string[] split = info.Split((char[])null,StringSplitOptions.RemoveEmptyEntries);

Like so:

static void Main(string[] args)
{
    var info = "628      5911.3097      1660.0134      3771.8285              0";
    Console.WriteLine(info);
    Console.WriteLine();

    string[] split = info.Split((char[])null,StringSplitOptions.RemoveEmptyEntries);
    foreach (string s in split)
        Console.WriteLine("\"" + s + "\" is empty: " + (s.Length == 0));

    //What happens if we concat the strings?
    Console.WriteLine();
    Console.WriteLine(string.Concat(split));

    Console.ReadLine();

    /*
    628      5911.3097      1660.0134      3771.8285              0

    "628" is empty: False
    "5911.3097" is empty: False
    "1660.0134" is empty: False
    "3771.8285" is empty: False
    "0" is empty: False

    6285911.30971660.01343771.82850
    */
}
NPSF3000
  • 2,421
  • 15
  • 20