String.LastIndexOf的经历

王朝c#·作者佚名  2006-12-17
窄屏简体版  字體: |||超大  

C#里的String类里有个LastIndexOf方法,其中一个重载方法

MSDN里这样描述:

public int LastIndexOf(

char value,

int startIndex,

int count

);

A Unicode character to seek. startIndex The starting position of a substring within this instance. count The number of character positions to examine.

我在使用时,我想从字符串dd/fff/fff/>的子串dd/fff/fff里找到最后一个“/”,即倒数第二个“/”于是:

string strTest=”dd/fff/fff/>”;

int iPos=strTest.LastIndexOf('/',0,strTest.Length-2);

但是在执行时老是报“ArgumentOutOfRangeException”的错误,后来想到了,原来LastIndexOf方法检查字符串的方向是从右向左的,因此,要以右边为基准:

int iPos=strTest.LastIndexOf('/',strTest.LastIndexOf('/')-1,strTest.Length-2);

平时都是以左边为开始的,所以碰到逆向思维时,很容易出错!

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航