本帖最后由 吴超 于 2012-6-3 11:03 编辑
- <FONT color=red>indexOf()的字符串所在的位置,在哪个位置就返回所在位置减去1(这是角标),有重复按顺序得出第一个。
- 如果没有找到, 则返回 -1。</FONT>
- <FONT color=red>下面是源码,没复制完全,有兴趣可以参考,可以提高自己编码水平
- </FONT>public int indexOf(int ch, int fromIndex) {
- int max = offset + count;
- char v[] = value;
- if (fromIndex < 0) {
- fromIndex = 0;
- } else if (fromIndex >= count) {
- // Note: fromIndex might be near -1>>>1.
- return -1;
- }
- int i = offset + fromIndex;
- if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
- // handle most cases here (ch is a BMP code point or a
- // negative value (invalid code point))
- for (; i < max ; i++) {
- if (v[i] == ch) {
- return i - offset;
- }
- }
- return -1;
- }
- if (ch <= Character.MAX_CODE_POINT) {
- // handle supplementary characters here
- char[] surrogates = Character.toChars(ch);
- for (; i < max; i++) {
- if (v[i] == surrogates[0]) {
- if (i + 1 == max) {
- break;
- }
- if (v[i+1] == surrogates[1]) {
- return i - offset;
- }
- }
- }
- }
- return -1;
- }
复制代码 |