C#基础 string Split 用字符分割字符串,删除空白项

未删除空白项

using System;

namespace ConsoleApp
{

    class Program
    {

        static void Main(string[] args)
        {
            string number = "0,,1..2,,3;;456789";
            string[] strs = number.Split(new char[] { ',', '.', ';' });
            foreach (var item in strs)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }

    }
}

删除空白项 - StringSplitOptions.RemoveEmptyEntries

using System;

namespace ConsoleApp
{

    class Program
    {

        static void Main(string[] args)
        {
            string number = "0,,1..2,,3;;456789";
            string[] strs = number.Split(new char[] { ',', '.', ';' },
                                         options: StringSplitOptions.RemoveEmptyEntries);
            foreach (var item in strs)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }

    }
}

 

posted @ 2021-03-10 16:13  DerekHan  阅读(891)  评论(0编辑  收藏  举报