第一种方法:

      使用 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

相关文章:

  • 2021-12-17
  • 2022-12-23
  • 2021-11-17
  • 2021-10-22
  • 2021-11-17
  • 2021-11-21
猜你喜欢
  • 2021-06-04
  • 2022-12-23
  • 2022-12-23
  • 2022-02-17
  • 2021-11-17
  • 2021-12-12
相关资源
相似解决方案