(急)C#去字符串除中间空格

来源:百度知道 编辑:UC知道 时间:2024/05/26 09:38:38
一个string类型的字符串,把它的中间空格去掉。就象trim()那样。
最好给出代码,不要foreach和replace
我是想对一个字符串求出有多少个字节,如果连续空格多了怎么用repale。

一个字符串求出有多少个字节?你是想求字母数吧?
我的方法可以把字符串里面所有空格去掉。
string YuanStr = "a b c d dde es";//源字符串
string NewStr = "";//新字符串
CharEnumerator CE = YuanStr.GetEnumerator();
while (CE.MoveNext())
{
byte[] array = new byte[1];
array = System.Text.Encoding.ASCII.GetBytes(CE.Current.ToString());
int AsciiCode = (short)(array[0]);
if (AsciiCode != 32)
{
NewStr += CE.Current.ToString();
}
}
Console.WriteLine(YuanStr);
Console.WriteLine(NewStr);

string str="abc de fg";
string str1=str.trim();
response.write(str1);

result:
abcdefg

CharEnumerator CEnumerator = textBox1.Text.GetEnumerator();
while (CEnumerator.MoveNext())
{
byte[] a