当前位置:K88软件开发文章中心编程语言.NET.NET01 → 文章内容

如何去除字符串中间的空格

减小字体 增大字体 作者:佚名  来源:翔宇亭IT乐园  发布时间:2018-12-31 11:26:32

: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);
    }
}

运行结果如下:



如何去除字符串中间的空格