标签:返回 win bmp static com ima eclips character stat
os :windows7 x64
jdk:jdk-8u131-windows-x64
ide:Eclipse Oxygen Release (4.7.0)
code:
package jizuiku.t00;
public class Demo4 {
public static void main(String[] args) {
// 索引值 0123
String str = "abc01234543210cba";
char ch = ‘0‘;
System.out.println(str.indexOf(ch,3));//包括索引值为3的字符
//那样是指定索引值大于字符串长度呢?
System.out.println(str.indexOf(ch,20));//返回-1,看了源代码懂了
}
}
result:

scoureCode:
public int indexOf(int ch, int fromIndex) {
final int max = value.length;
if (fromIndex < 0) {
fromIndex = 0;
} else if (fromIndex >= max) {
// Note: fromIndex might be near -1>>>1.
return -1;
}
if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
// handle most cases here (ch is a BMP code point or a
// negative value (invalid code point))
final char[] value = this.value;
for (int i = fromIndex; i < max; i++) {
if (value[i] == ch) {
return i;
}
}
return -1;
} else {
return indexOfSupplementary(ch, fromIndex);
}
}
Java优秀,值得学习。
学习资源:API手册+Java源码。
JavaSE8基础 String indexOf 正向 从指定索引值开始查找 字符在字符串中第一次出现的位置
标签:返回 win bmp static com ima eclips character stat
原文地址:http://www.cnblogs.com/jizuiku/p/7468938.html