c语言 string indexof,String类的indexOf()方法

/**

Returns the index within this string of the first occurrence of the

specified character, starting the search at the specified index.

返回这个字符串从指定的下标开始,指定字符在这个字符串中第一次出现的索引。

If a character with value ch occurs in the

character sequence represented by this String

object at an index no smaller than fromIndex, then

the index of the first such occurrence is returned.

如果一个字符,其值为ch,出现在字符串序列由这个字符串对象,其下标如果不小于fromIndex,

那么第一次出现该字符的下标索引会被返回

For values of ch in the range from 0 to 0xFFFF (inclusive),

this is the smallest value k such that:

(this.charAt(k) == ch) && (k <= fromIndex) is true.

For other values of ch, it is the smallest value k such that:

(this.codePointAt(k) == ch) && (k <= fromIndex)

is true.

In either case, if no such character occurs in this

string at or after position fromIndex, then

-1 is returned.

不论哪种情况,如果在fromIndex之后,没有这样的字符出现了,那么返回-1.

There is no restriction on the value of fromIndex. If it

is negative, it has the same effect as if it were zero: this entire

string may be searched. If it is greater than the length of this

string, it has the same effect as if it were equal to the length of

this string: -1 is returned.

All indices are specified in {@code char} values

(Unicode code units).

@param ch a character (Unicode code point).

@param fromIndex the index to start the search from.

@return the index of the first occurrence of the character in the

character sequence represented by this object that is greater

than or equal to {@code fromIndex}, or {@code -1}

if the character does not occur.

*/

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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值