1.
using System.Text.RegularExpressions;
string str2 = str1.Trim();//去字符串头尾
string str3 = Regex.Replace(str2, @" * ", " ");//" * " //就可以替换连续空格
2.
下面是自己写的去连续空格,但还是要将字符串先去头尾空格
str1 = str1.Trim();//
string str2 = "";
for (int i = 0; i < str1.Length; i++)//不能对获得的字符进行修改,即str1[0].ToString()="0"是错误的
{
if (str1[i].ToString() == " ")
{
for (int j = i + 1; str1[j].ToString() == " "; j++)
{
i = j; //追加连续空格的最后个空格
}
}
str2 = str2 + str1[i];
}
string str3 = str2;
3.
自己编写的去头所有空格和中间连续空格为单一空格
string str2 = "";
int i1=0;
if (i1 == 0)
{
if (str1[i1].ToString() == " ")
{
for (int j = 1; str1[j].ToString() == " "; j++)
{ i1 = j; }
}
else
{ str2 = str1[0].ToString(); }
i1=i1+1;
}
if(i1>=1)
{
for (int k = i1; k <str1.Length; k++)
{
if (str1[k].ToString() ==" ")
{
for (int l = k + 1; str1[l].ToString() == " "; l++)
{
k = l; //追加连续空格的最后个空格
}
}
str2 = str2 + str1[k];
}
}
using System.Text.RegularExpressions;
string str2 = str1.Trim();//去字符串头尾
string str3 = Regex.Replace(str2, @" * ", " ");//" * " //就可以替换连续空格
2.
下面是自己写的去连续空格,但还是要将字符串先去头尾空格
str1 = str1.Trim();//
string str2 = "";
for (int i = 0; i < str1.Length; i++)//不能对获得的字符进行修改,即str1[0].ToString()="0"是错误的
{
if (str1[i].ToString() == " ")
{
for (int j = i + 1; str1[j].ToString() == " "; j++)
{
i = j; //追加连续空格的最后个空格
}
}
str2 = str2 + str1[i];
}
string str3 = str2;
3.
自己编写的去头所有空格和中间连续空格为单一空格
string str2 = "";
int i1=0;
if (i1 == 0)
{
if (str1[i1].ToString() == " ")
{
for (int j = 1; str1[j].ToString() == " "; j++)
{ i1 = j; }
}
else
{ str2 = str1[0].ToString(); }
i1=i1+1;
}
if(i1>=1)
{
for (int k = i1; k <str1.Length; k++)
{
if (str1[k].ToString() ==" ")
{
for (int l = k + 1; str1[l].ToString() == " "; l++)
{
k = l; //追加连续空格的最后个空格
}
}
str2 = str2 + str1[k];
}
}