职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 1040|回复: 4

c#一个字符串里有多个不同的空格怎么转化成都是一个空格

[复制链接]
jason_dion 发表于 2009-8-17 09:22 | 显示全部楼层 |阅读模式
例如\"ab a bb cc\"
转化成\"ab a bb cc \"
力仔 发表于 2009-8-17 09:23 | 显示全部楼层

c#一个字符串里有多个不同的空格怎么转化成都是一个空格

正则替换
yx 发表于 2009-8-17 09:23 | 显示全部楼层

c#一个字符串里有多个不同的空格怎么转化成都是一个空格

用记事本方式打开
在编辑菜单里
利用替换功能实现即可
烈火奥拓仔 发表于 2009-8-17 09:23 | 显示全部楼层

c#一个字符串里有多个不同的空格怎么转化成都是一个空格

using System;
using System.Text.RegularExpressions;

class MyClass
{
static void Main(string[] args)
{
string sInput, sRegex;

sInput = "aa bc de ";

sRegex = "[ ]+";

Regex r = new Regex(sRegex);

MyClass c = new MyClass();

// Assign the replace method to the MatchEvaluator delegate.
MatchEvaluator myEvaluator = new MatchEvaluator(c.ReplaceCC);

// Write out the original string.
Console.WriteLine(sInput);

// Replace matched characters using the delegate method.
sInput = r.Replace(sInput, myEvaluator);

// Write out the modified string.
Console.WriteLine(sInput);
}

public string ReplaceCC(Match m)
// Replace each Regex cc match with the number of the occurrence.
{
i++;
return " ";
}
public static int i = 0;
}

参考资料: http://msdn.microsoft.com/zh-cn/ ... matchevaluator.aspx
★德仔★ 发表于 2009-8-17 09:23 | 显示全部楼层

c#一个字符串里有多个不同的空格怎么转化成都是一个空格

string str = "ab abb ccc";
while (str.IndexOf("") > 0)
{
str.Replace("", " ");
}
您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-4-27 16:59 , Processed in 0.149303 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表