java 查找子串算法_Java 中字符串的子串搜索

基友前两天参加了阿里的实习生面试,问了个问题,就是关于字符串的子串搜索的问题。想想实现方式无非就是两层循环,但是 java 中是有现成实现的,于是我就去查查源码,看看 java 语言怎么实现这个的,发现也就是差不多的意思。

java.lang 包中 String 类 有几个 indexOf() 函数,我要寻找的是 indexOf(String str) 这个的具体实现,发现了

public int indexOf(String str) {

return indexOf(str, 0);

}

然后 F3 继续找,public int indexOf(String str, int fromIndex) {

return indexOf(value, offset, count, str.value, str.offset, str.count,

fromIndex);

}这个调用应该就是算法的实现了,继续 F3

/**

* Code shared by String and StringBuffer to do searches. The source is the

* character array being searched, and the target is the string being

* searched for.

*

* @param source

* the characters being searched.

* @param sourceOffset

* offset of the source string.

* @param sourceCount

* count of the source string.

* @param target

* the characters being searched for.

* @param targetOffset

* offset of the target string.

* @param targetCount

* count of the target string.

* @param fromIndex

* the index to begin searching from.

*/

static int indexOf(char[] source, int sourceOffset, int sourceCount,

char[] t

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值