日期:2014-05-20  浏览次数:20687 次

idexOf(char) indexOf(string,int) 为何结果一样?
String s ="java is a"+"platform independent language";
System.out.println("index of t="+s.indexOf('t'));//12
System.out.println("last index of t="+s.lastIndexOf('t')); //27
System.out.println("index of(t,10)="+s.indexOf('t',10)); //12 为什么不是2呢,请指教
System.out.println("last index of(t,60=)"+s.lastIndexOf('t',60)); //27 这里也搞不懂为什么是这个结果
请大家赐教,非常感谢。

------解决方案--------------------
他们返回的结果都是从起始位置开始算起的 懂了么?比如023451234 你从0开始返回第一个1的index是5 而你从5开始返回第一个1的index也是5 这是相对于整个字符串而言的
------解决方案--------------------

------解决方案--------------------
它返回的是在字符串中的位置,跟你从哪里开始找是无关的
------解决方案--------------------
idexOf(char) indexOf(string,int),都是首次出现的地址,结果是一个整型。
------解决方案--------------------
探讨
非常感谢你们,前面的都搞懂了,但是这个从第60个开始找,这个字符串根本没有那么长,这个是这么算的呢。
System.out.println("last index of(t,60=)"+s.lastIndexOf('t',60)); //27 这里也搞不懂为什么是这个结果