Lisong's Blog


十月 24th, 2008

c# Replace

Learning, by 木公.

今天在写一个小函数时遇到个囧事,被string.Replace给拦住了。
string.Replace是要求有返回值的,由于昨天刚刚结束一个asp程序,就习惯的写成了string.Replace(old value,new value),结果浪费了我N久的时间找错误,郁闷。

Old Code:

1
2
3
4
5
6
7
8
9
private string cutStringBlank(string strline)
{
    strline=strline.Trim();
    do
    {
        strline.Replace('  ',' ');
    }while(strline.IndexOf(' ')!=-1);
    return strline;
}

这是用来将一含有不定长度空格的字符串转换成间隔空格为1个字符的函数,以便于能够string.Split()

New Code:

1
2
3
4
5
6
7
8
9
private string cutStringBlank(string strline)
{
    strline=strline.Trim();
    do
    {
        strline=strline.Replace('  ',' ');
    }while(strline.IndexOf(' ')!=-1);
    return strline;
}

特别要注意的是,Old Code在编译和运行时是没有错误的,所以我一直没有找到这个错误所在[:face_39:]

Back Top

回复自“c# Replace”

评论 (2) 引用 (0) 发表评论 引用地址
  1. 很好,就是這個問題,呵呵

  2. I put a WordPress blog on my website a few days ago, and I was just curious about how it works. So I just want to know if all the posts are saved into a single file or if they are separate for each post. Then I also want to know where they can be found on my server. Thanks.

  1. 没有任何引用。

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*


无觅相关文章插件,快速提升流量