本帖最后由 Star 于 2011-2-22 11:08 编辑 7 p% I3 i; c R& r1 ?( r% Y7 F7 X" i
这一久在给EB_EXP debug,有个地方需要对字符串进行Insert处理,原来的程序是这样写的:
* x) T# S& `) S$ [: \/ M
result.Insert(result.IndexOf("</p>"), " (单位:分钟)");/ q( ]* C% m# f7 U
复制代码
调试过程中,发现返回的这个result的值没有改变,仍然是执行Insert操作之前的值,于是去网上查,MSDN(http://msdn.microsoft.com/en-us/library/system.string.insert.aspx)上有一条注意说明:( P; B5 B7 e7 T, }0 R$ P& v: P! g3 Q# K
Note 3 S7 G5 Q7 |7 d# t- r This method does not modify the value of the current instance. Instead, it returns a new string in which value is inserted into the current instance.2 ?, N& s2 x& s) { Y5 U. o2 Q9 m
) y- }! G: T5 x& p
看了这句话就知道了,原来string的Insert操作没有修改result的值,所以返回result的话,是不对的,应该返回Insert方法的返回值,如下:
2 _- i' h. T8 s/ C/ K2 h
result = result.Insert(result.IndexOf("</p>"), " (单位:分钟)"); ; k4 x5 C" \; t