【转载】C#处理空格和换行

使用C#处理字符串是一个常见的情况,当字符串中含有空格或者换行符号的时候,如果业务需要,我们可以通过相应的方法将之处理掉,处理成不含空格和换行符号的字符串,处理的过程使用到正则表达式。

具体函数处理的过程如下:

static void Main(string[] args)
        {
            List<string> list = new List<string>();
            list.Add("\r \n aab bcc");
            list.Add("aab bcc \r  \ndd");
            list.Add("aa\r b b \n cc");
            
            Regex regex = new Regex(@"\s");
            for (int i = 0; i < list.Count;i++ )
            {
                string str = list[i];
                list[i]=regex.Replace(str,"",str.Length);
            }

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

 

扩展阅读:目前国内主流的云服务器厂商有哪些_IT技术小趣屋

备注:原文转载自C#处理空格和换行_IT技术小趣屋

posted @ 2019-03-31 14:42  江湖逍遥  阅读(4955)  评论(0编辑  收藏  举报