第一种方法:

使用 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#正则表达式合并连续空格为单个空格的更多相关文章

  1. python核心编程正则表达式练习题1-2匹配由单个空格分隔的任意单词对,也就是性和名

    # 匹配由单个空格分隔的任意单词对,也就是姓和名 import re patt = '[A-Za-z]+ [A-Za-z]+' # 方法一 +加号操作符匹配它左边的正则表达式至少出现一次的情况 # p ...

  2. c# 字符串中多个连续空格转为一个空格

    #region 字符串中多个连续空格转为一个空格 /// <summary> /// 字符串中多个连续空格转为一个空格 /// </summary> /// <param ...

  3. 《c程序设计语言》读书笔记--多个空格变为一个空格

    #include <stdio.h> int main() { int c; int flag = 0; while((c = getchar()) != EOF) { if(c == ' ...

  4. js中去掉字符中间空格和首尾空格

    转载: https://www.jb51.net/article/109522.htm 1.  去掉字符串前后所有空格: 代码如下: ? 1 2 3 4 function Trim(str)  {   ...

  5. 用JS去掉前后空格或中间空格大全

    1.  去掉字符串前后所有空格: -- js实现trim功能 //去除字符串前后所有空 function Trim(str) { return str.replace(/(^\s*)|(\s*$)/g ...

  6. js校验规则--去空格、加空格

    为了更加直观,有些号码需要加空格: // 拼接空格,每4位加一个空格 let bankAccount = '6228888888888888888'; let blank_value = bankAc ...

  7. 【ABAP系列】SAP smartforms金额字段产生空格,除去空格的方法

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP smartforms金额 ...

  8. CSS 标签、类名和ID选择器有空格和没有空格的区别

    前言 标签选择器.类名选择器以及 ID 选择器在使用上要注意"加空格"和"不加空格".两种使用形式上有很大的差别. 下面是讲解案例的 HTML 代码: < ...

  9. php 将字符串中的连续多个空格转换为一个空格

    转载自:http://www.phpernote.com/php-function/633.html /** * 多个连续空格只保留一个 * * @param string $string 待转换的字 ...

随机推荐

  1. java web前端调试手段

    1.console.log() 2. jQuery.ajax({                     url:"/task1/com/guodiantong/servlet/JsonTo ...

  2. [jquery-ajax] jquery ajax 三种情况对比

    <button class="btn1">async:false</button> <button class="btn2"> ...

  3. C#: 线程间操作无效: 从不是创建控件“dataGridView”的线程访问它

    最近在修改自动化小工具,用多线程来解决后台拷贝导致WinForm界面卡死的情况,但是遇到过错:线程间操作无效: 从不是创建控件“dataGridView”的线程访问它. 这是因为在多线程程序中,新创建 ...

  4. ABP框架入门踩坑-使用MySQL

    使用MySQL ABP踩坑记录-目录 起因 因为我自用的服务器只是腾讯云1核1G的学生机,不方便装SQL Server,所以转而MySQL. 这里使用的MySQL版本号为 8.0. 解决方案 删除Qi ...

  5. Day 43数据库(Day1)

    创建表. create table student( id int not null auto_increment PRIMARY key, name archar() not null, age i ...

  6. C++类模板 template <class T>

    C++在发展的后期增加了模板(template )的功能,提供了解决这类问题的途径.可以声明一个通用的类模板,它可以有一个或多个虚拟的类型参数. 比如: class Compare_int class ...

  7. Binary Search-483. Smallest Good Base

    For an integer n, we call k>=2 a good base of n, if all digits of n base k are 1. Now given a str ...

  8. python操作mongodb实例

    安装pymongo扩展 import pymongo; client = pymongo.MongoClient(host='10.48.176.170',port=27017); db = clie ...

  9. POJ 1146

    #include <iostream> #include <algorithm> #define MAXN 55 using namespace std; char _m[MA ...

  10. Java Web入门学习(四)Eclipse与Maven、Tomcat整合配置 (重整版并解决问题)

    Java Web学习(四)Eclipse与Maven整合配置 (重整版) 一.准备工作 1.Tomcat 8.5.15 2.Maven3.5 3.Eclipse Neon.3 Release (4.6 ...