The method below recursively checks if a string contains another string. Example: contains(hello, lo, 0) should return true, while contains(hello, eh, 0) should return false. The method itself works, but I was wondering how it could be made more efficient/generally better.
Method:
public static boolean contains(String word1, String word2, int index)
{
if((word1 == null) || (word2 == null)){
return false;
}
if(index + (word2.length() - 1) >= word1.length()){
return false;
}
int count = 0, j = 0;
for(int i = index; i < (word2.length() + index); i++){
if(word1.charAt(i) == word2.charAt(j)){
if((i != 0) && (j != 0)){
if(word1.charAt(i - 1) == word2.charAt(j - 1)){
count++;
}
} else {
count++;
}
}
j++;
}
if(count == word2.length()){
return true;
}
return contains(word1, word2, index + 1);
}