- ·上一篇文章:实例讲解C#控件StatusStrip
- ·下一篇文章:C#中的字符串类
如何去除字符串中间的空格
:2010-05-08 10:19:00
使用Trim只能去除字符串前端和后端的空ge,那么字符串中间的空ge如何去掉呢?下面给出一个完整的程序:
using System;
using System.Collections.Generic;
using System.Text;
class TrimAll
{
string trimAllSpace(string str)
{
string temp = "";
for (int i = 0; i < str.Length; i++)
if (str[i] != ' ')
temp += str[i].ToString();
return temp;
}
public static void Main()
{
TrimAll ta = new TrimAll();
string testStr = "I Love China! I Love Chinese People!";
string reStr = ta.trimAllSpace(testStr);
Console.WriteLine("源字符串:" + testStr);
Console.WriteLine("去掉空ge后的字符串:"+reStr);
}
}
运行结果如下:
!
如何去除字符串中间的空格