判断一个字符串是否包含另一个字符串(用java但是不能用index()这个函数)

72 阅读1分钟

目录: 
一.方法介绍 
二.图示意 
三.源代码 

一.方法介绍 

判断一个字符串str1是否包含另一个字符串str2: 
1.取str2的第一个字符一次和str1的字符依次比较,知道找到相等的字符为止或者找完整个str1的length. 

 

2.当找到相等的字符后,在str2长度内str2与str1依次进行比较 

 

二.图示意

三.源代码 

JDK中java.lang.String.indexOf(char[], int, int, char[], int, int, int)源码: 

 

/**
     * 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 so