System.String.Split(null) doesn't remove whitespace (C#)

前端 未结 1 457
攒了一身酷
攒了一身酷 2021-01-14 13:39

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

相关标签:
1条回答
  • 2021-01-14 14:32

    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
        */
    }
    
    0 讨论(0)
提交回复
热议问题