标题:[求助]c#中的split怎么区别一个空格和两个空格?
只看楼主
刚子1984
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2020-11-8
得分:0 
static void Main(string[] args)
        {
            string str = @"F:\test.txt";
            string[] n = File.ReadAllLines(str);
            for (int i = 0; i < n.Length; ++i)
            {
                n[i] = n[i].Trim();
            }
            string[]m = System.Text.RegularExpressions.Regex.Split(n[0], @"\s{1,}");
            string[,] A = new string[n.Length, m.Length];
            for (int i = 0; i < n.Length; ++i)
            {
                for (int j = 0; j < m.Length; ++j)
                {
                    string[] s = System.Text.RegularExpressions.Regex.Split(n[i], @"\s{1,}");
                    A[i, j] = s[j];
                }
            }
            for(int i = 0; i < n.Length; ++i)
            {
                for (int j = 0; j < m.Length; ++j)
                {
                    Console.Write(A[i, j] + ' ');
                }
                Console.WriteLine();
            }
            int[,]B=new int[n.Length, m.Length];
            for (int i = 0; i < n.Length; ++i)
            {
                for (int j = 0; j < m.Length; ++j)
                {
                    B[i, j] = int.Parse(A[i,j]);
                }
            }
            for (int i = 0; i < n.Length; ++i)
            {
                for (int j = 0; j < m.Length; ++j)
                {
                    Console.Write("{0} ",B[i,j]);
                }
                Console.WriteLine();
            }
        }
2020-11-08 16:09
刚子1984
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2020-11-8
得分:0 
试试上面这个,亲测有效
2020-11-08 16:11



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-54092-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.276449 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved