-3
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?

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
shaILU
  • 2,050
  • 3
  • 21
  • 40

7 Answers7

4

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

RoflcoptrException
  • 51,941
  • 35
  • 152
  • 200
2

because in 1st string you are having forgotPwd and in second forgetPwd . Difference is o and e

dku.rajkumar
  • 18,414
  • 7
  • 41
  • 58
1

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
Vaandu
  • 4,857
  • 12
  • 49
  • 75
1

case 1 is obvious.
case 2 is false because s1 has "forgot" and s2 has "forget".

Gaurav
  • 1,549
  • 2
  • 15
  • 31
1

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.

ArVan
  • 4,225
  • 8
  • 36
  • 58
1

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";

Wilmer
  • 1,025
  • 5
  • 9
-1

Maybe you could try specifying an index of the form

        "Foobar".startsWith("bar", 3) 

which returns true. After checking your typos of course.

seeker
  • 6,841
  • 24
  • 64
  • 100