求个去掉重复空格的方法 需要从文本文件中逐行读取数据。多数情况下读取出的数据中包含重复的空格,如:“张三 30 男 汉族”,需要转换成“张三 30 男 汉族”的形式以进行下面的操作。请教是否有高效的方法? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 string s = "a b c"; string[] x = s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); string y = string.Join(" ", x); replace(" "," ")试试这个?把两个空格替换成1个空格。 string test = "张三 30 男 汉 族";test = System.Text.RegularExpressions.Regex.Replace(test, @"[ ]+", " "); 试试这个\s代表空格或者Tab之类的分隔符,有两个或者两个以上就替换为一个空格[code]string test = "张三 30 男 汉 族";test = System.Text.RegularExpressions.Regex.Replace(test, @"\s{2,}", " ");[/code] 2楼的不错string s = "a b c"; string[] x = s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); string y = string.Join(" ", x);但是9,11楼的也行啊replace(" "," ") 试试这个?把两个空格替换成1个空格。string test = "张三 30 男 汉 族";test = System.Text.RegularExpressions.Regex.Replace(test, @"[ ]+", " ");楼主去选择吧 string test = "张三 30 男 汉 族"; string s = Regex.Replace(test, @"\s", string.Empty); Response.Write(s); //高 实在是高 二楼 我爱你//高 实在是高 二楼 我爱你//高 实在是高 二楼 我爱你//高 实在是高 二楼 我爱你string s = "a b c"; string[] x = s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); string y = string.Join(" ", x); string test = "张三 30 男 汉 族";string s = Regex.Replace(test, @"\s", " "); string test = "张三 30 男 汉 族";string s = Regex.Replace(test, @"\s{2,}", " "); 哦另外,直接Replace(" ", " ")能将两个空格变成一个,但是似乎对于三、四个空格的情况似乎还是会有连续的空格,所以恐怕直接Replace比较困难,至少可能需要做个循环什么的。 sql 语句怎么写!!!! falsh缓存问题···· select multiple 中text的值存入数据库的代码怎么编写 怎么看同一个服务器分两个ip? Gridview模板列的问题。。 文本处理问题~急!!!!! asp.net网站 无法访问文件,请教! 图片怎么读取? 怎樣用javascript引發服務器上指定的方法 关于datagrid插入新行的问题(较难),100分送上 这个漂浮的东东要怎么搞?? 多条数据插入多张表中
string[] x = s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
string y = string.Join(" ", x);
试试这个?把两个空格替换成1个空格。
test = System.Text.RegularExpressions.Regex.Replace(test, @"[ ]+", " ");
[code]
string test = "张三 30 男 汉 族";
test = System.Text.RegularExpressions.Regex.Replace(test, @"\s{2,}", " ");
[/code]
string s = "a b c";
string[] x = s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
string y = string.Join(" ", x);但是9,11楼的也行啊
replace(" "," ")
试试这个?把两个空格替换成1个空格。string test = "张三 30 男 汉 族";
test = System.Text.RegularExpressions.Regex.Replace(test, @"[ ]+", " ");
楼主去选择吧
string s = Regex.Replace(test, @"\s", string.Empty);
Response.Write(s);
//高 实在是高 二楼 我爱你
//高 实在是高 二楼 我爱你
//高 实在是高 二楼 我爱你
string s = "a b c";
string[] x = s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
string y = string.Join(" ", x);
string test = "张三 30 男 汉 族";
string s = Regex.Replace(test, @"\s", " ");
string test = "张三 30 男 汉 族";
string s = Regex.Replace(test, @"\s{2,}", " ");