码迷,mamicode.com
首页 > Windows程序 > 详细

C#正则表达式合并连续空格为单个空格

时间:2016-04-21 15:12:36      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:

第一种方法:

      使用 System.Text.RegularExpressions.Regex.Replace()方法

  string result = String.Empty;

  string str = "Just     Test the  Method";
  result = Regex.Replace(str, "\\s{2,}", " ");//s{2,} 中的s表示空格,数字2表示两个或以上的空格

      MessageBox.Show(result);//结果是:Just Test the Method

 

 

第二种方法:

 

  string result = String.Empty;

  string str = "Just     Test the  Method";

  Regex replaceSpace = new Regex(@"\s{1,}", RegexOptions.IgnoreCase);

  result = replaceSpace.Replace(str, " ").Trim();

      MessageBox.Show(result);//结果是:Just Test the Method

 

参考:http://www.cnblogs.com/computer-lzy/archive/2011/12/07/2279417.html

C#正则表达式合并连续空格为单个空格

标签:

原文地址:http://www.cnblogs.com/tommy-huang/p/5416840.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!