本帖最后由 Star 于 2011-2-22 11:08 编辑
* b% ?* O) l1 P. g/ D
1 M" x2 P, U1 z u2 I) s这一久在给EB_EXP debug,有个地方需要对字符串进行Insert处理,原来的程序是这样写的:- f- c7 B% p# e, z I
- result.Insert(result.IndexOf("</p>"), " (单位:分钟)");
; ^5 S* a. |* G2 h5 S
复制代码 调试过程中,发现返回的这个result的值没有改变,仍然是执行Insert操作之前的值,于是去网上查,MSDN(http://msdn.microsoft.com/en-us/library/system.string.insert.aspx)上有一条注意说明:
7 M, Q4 t: A6 n+ }3 Z- G Note
9 I8 N4 x/ |; ~9 S. A 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.0 P) |4 X4 y, s6 b
+ R9 f2 R0 G$ @0 G看了这句话就知道了,原来string的Insert操作没有修改result的值,所以返回result的话,是不对的,应该返回Insert方法的返回值,如下:
/ m0 @, {. Z4 c4 x, m- result = result.Insert(result.IndexOf("</p>"), " (单位:分钟)");
) [; a, b3 V& S2 A' Z6 g' f
复制代码 |