if (retVal = true) {
// Returns -1 if not matched.
System.out.println(string2 + " is a substring of " + string1);
} else {
System.out.println(string2 + " is not a substring of " + string1);
}
}
}
1
Expert's answer
2015-07-10T02:25:15-0400
Pay your attention to this code line: retVal = string1.equals(string2); Method "equals" return true if and only if those strings are equal. If you want to check if one string is a substring of another, you can do following: 1) Use method "contains". I'd prefer this way. String implements CharSequence interface, so there shouldn't be any problems. https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#contains%28java.lang.CharSequence%29 2) If you are familiar with regular expressions, there is a method "matches". https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#matches%28java.lang.String%29
Comments
Leave a comment