本帖最后由 Star 于 2011-2-22 11:08 编辑 V1 X e* s/ v3 s$ E5 V+ e4 _
1 [, w! N8 E, ?" F U" f2 H这一久在给EB_EXP debug,有个地方需要对字符串进行Insert处理,原来的程序是这样写的:
8 ~% Q+ G6 w5 ~/ ^' i) U
result.Insert(result.IndexOf("</p>"), " (单位:分钟)"); " z! @: @3 G3 X/ I
复制代码
调试过程中,发现返回的这个result的值没有改变,仍然是执行Insert操作之前的值,于是去网上查,MSDN(http://msdn.microsoft.com/en-us/library/system.string.insert.aspx)上有一条注意说明:# V/ i X9 b7 N6 A; Q
Note g% E* y" W @0 g9 G$ N: Y 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. 3 n2 P* v6 W& c0 r3 I% Z9 B1 b . r# Y0 w8 C2 `- i( o; F% k' k看了这句话就知道了,原来string的Insert操作没有修改result的值,所以返回result的话,是不对的,应该返回Insert方法的返回值,如下:
/ J8 V: r, T, x( z Z# }
result = result.Insert(result.IndexOf("</p>"), " (单位:分钟)");9 d1 v3 q! c3 w1 H2 }( N6 M; ?6 u