String java startsWith
-
13-04-2021 - |
Question
public class Test {
public static void main(String[] args) throws Exception {
String s1 = "/program/rest/user/forgotPwd/";
String s2 = "/program/rest/user/forgetPwd/test";
System.out.println(s1.startsWith(s2));
System.out.println(s2.startsWith(s1));
}
}
Both the cases print false
. Any explanations?
La solution
The strings are not as identical as you think. The first string contains forgotPwd
while the second contains forgetPwd
.
Autres conseils
because in 1st string you are having forgotPwd
and in second forgetPwd
. Difference is o
and e
Your s1 is not a part of s2. forgotPwd
Check this.
String s1 = "/program/rest/user/forgotPwd/";
String s2 = "/program/rest/user/forgotPwd/test";
System.out.println(s1.startsWith(s2));
System.out.println(s2.startsWith(s1));
Output:
false
true
case 1 is obvious.
case 2 is false because s1 has "forgot" and s2 has "forget".
That's natural. Look closer at your strings.
String s1 = "/program/rest/user/forgotPwd/";
String s2 = "/program/rest/user/forgetPwd/test";
s1 has the word forgotPwd
, s2 has forgetPwd
. There is 1 letter difference.
You have a typo in s2 it says:
String s2 = "/program/rest/user/forgetPwd/test";
and it should say:
String s2 = "/program/rest/user/forgotPwd/test";
Maybe you could try specifying an index of the form
"Foobar".startsWith("bar", 3)
which returns true. After checking your typos of course.