- ·上一篇内容:C#的装箱与拆箱
- ·下一篇内容:2010年母亲节祝福短信(第一辑)
如何去除字符串中间的空格
使用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);
}
}
运行结果如下:
本文源自:翔宇亭——IT乐园(http://www.biye5u.com),转载请保留此信息!
微信搜索“优雅的代码”关注本站的公众号,或直接使用微信扫描下面二维码关注本站公众号,以获取最新内容。
个人成长离不开各位的关注,你的关注就是我继续前行的动力。