截取字符串问题,急急急急急!
怎么去掉1个字符串中间的多个空格,只留1个空格!字符串事例:"我 是 中 国 人"
想要的效果: "我 是 中 国 人"
高手帮忙啊! --------------------编程问答-------------------- Regex.Replace --------------------编程问答--------------------
using System;--------------------编程问答-------------------- 我用Replace怎么只能把中间所有空格去掉,但是我想留一个啊!
using System.Text.RegularExpressions;
class Test
{
static void Main()
{
string s0 = "我 是 中 国 人";
string s1 = Regex.Replace(s0, "\\s+", " ");
Console.WriteLine(s1);
}
}
麻烦高手解惑! --------------------编程问答--------------------
--------------------编程问答--------------------
string input = "我 是 中 国 人";
string str = Regex.Replace(input, @"( )\1+", "$1", RegexOptions.None);
Console.WriteLine(str);
Console.Read();
Console.WriteLine(Regex.Replace("我 是 中 国 人", @" {2,}", " "));--------------------编程问答--------------------
string result=System.Text.RegularExpressions.Replace("你的字符串","\\s+"," ");--------------------编程问答-------------------- 越来越不好蹭分了。 --------------------编程问答-------------------- string str="";
rgexp.Replace(str,"\s+"," "); --------------------编程问答-------------------- 顶 --------------------编程问答-------------------- 出手都好快啊...
同意7楼..呵呵.. --------------------编程问答-------------------- 来学习的.... --------------------编程问答-------------------- 你可以直接复制到你的VS中运行,我做出来了,你试试吧
string result = ""; //结果
string finalResult = "";//最终结果
string str = "我 是 中 国 人";//原始字符串
string[] str1 = str.Split(); //拆分成字符数组
for (int i = 0; i < str1.Length; i++)
{
result += str1[i].Trim(); //每个字符都trim一下,这样的结果字都会连住 "我是中国人"
}
//Console.WriteLine(result);
for (int i = 0; i < result.Length; i++)
{
finalResult += result[i] + " "; //加上空格
}
Console.WriteLine(finalResult); --------------------编程问答-------------------- trim --------------------编程问答--------------------
用的是正则的替换 匹配多个空格 替换为一个 --------------------编程问答-------------------- string Str1 = "我 是 中 国 人";
string result = Str1.Replace(" ", "");
Str1 = "";
for (int i = 0; i < result.Length; i++)
{
if (i < result.Length - 1)
Str1 += result[i] + " ";
else
Str1 += result[i];
}
textBox1.Text = Str1; --------------------编程问答--------------------
using System;--------------------编程问答--------------------
class test
{
static void Main()
{
string str="我 是 中 国 人";
str=str.Replace(" ","");
string[] arr=new string[str.Length];
for(int i=0;i<str.Length;i++)
{
arr[i]=str.Substring(i,1);
}
Console.WriteLine(string.Join(" ",arr));
}
}
using System;--------------------编程问答--------------------
class test
{
static void Main()
{
string str="我 是 中 国 人";
str=str.Replace(" ","");
string[] arr=new string[str.Length];
for(int i=0;i<str.Length;i++)
{
arr[i]=str.Substring(i,1);
}
Console.WriteLine(string.Join(" ",arr));
}
}
--------------------编程问答-------------------- 学习了 --------------------编程问答-------------------- ~~~zz~~
string str = "我 是 中 国 人"; //这是你转换前的字符
string strOK = ""; //这是你转换后的字符
char[] temp = str.ToCharArray();
for(int i = 0;i<temp.Length;i++)
{
if(temp[i].ToString() != " ")
{
strOK += temp[i].ToString() +" ";
}
}
strOK = strOK.Trim(); //着就是你最后得到的字 "我 是 中 国 人"
补充:.NET技术 , C#