We are about to switch to a new forum software. Until then we have removed the registration on this forum.
OK this is weird.
i have a string 12345.xxx
println(string.substring(string.length() - 3); // prints xxx to console
next line is:
if (string.substring(string.length() - 3) != "xxx") {
println(string + "yyy");
}
this is what prints to console:
12345.xxxyyy
It seems the if does not recognize the comparison!
I can't figure out why
Answers
So is it necessary to create the subStr for the expression in the if to evaluate correctly? Why doesn't this work as well? Just want to understand for future if conditions about how functions are evaluated within if while switch etc.
if ( !"xxx".equals(string.substring(string.length()-3)
No! I've declared variable subStr merely for aesthetic reasons, so the
if ()
expression isn't too big and more clear. :-jReference for String::equals() method at https://processing.org/reference/String_equals_.html already explains why we should avoid using equality operators for comparing String objects under Java. L-)
For discussion see: