I tried:
String test = "racecary";
StringBuilder stringBuilder = new StringBuilder(test);
System.out.println(stringBuilder.reverse()+" -------------");
if (stringBuilder.reverse().toString().equalsIgnoreCase(test)) {
System.out.println("Pass");
}else {
System.out.println("Fail");
}
It always prints pass, even when I mispelled racecar
, but passes when I use ==
inplace of .equalsIgnoreCase
.
Am I doing something wrong? I have already gone through following but didnt get the answer.
StringBuilder.reverse()
modifies the builder in place. The test printout is causing the string to be reversed twice, leaving it unmodified. Get rid of the printout and the code will work as expected.