문제

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?

도움이 되었습니까?

해결책

The strings are not as identical as you think. The first string contains forgotPwd while the second contains forgetPwd.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top