string.toUpperCase() behaving weirdly

I was trying to make an if statement that checks a string but isn’t case sensitive, and I came across some weird behavior:

String str = "Hello World.";

void setup(){
  println(str.toUpperCase()); //Outputs HELLO WORLD.
  
  println(str.toUpperCase() == "HELLO WORLD."); //Outputs false 
  println(str.toUpperCase() == str.toUpperCase()); //Outputs false (?!)

  //The same thing also happens to .toLowerCase()
}

Why do these second two print statements output false?
And how would I go about checking if a string matches in a condition which isn’t case sensitive?

Sidenote: this sort of code works in Javascript

1 Like

You can’t use == with String

Please use str.toUpperCase().equals(“Hello”) instead

4 Likes

Thanks.

1 Like

On String / Reference / Processing.org is explained why:

To compare the contents of two Strings, use the equals() method, as in if (a.equals(b)) , instead of if (a == b) . A String is an Object, so comparing them with the == operator only compares whether both Strings are stored in the same memory location. Using the equals() method will ensure that the actual contents are compared. (The troubleshooting reference has a longer explanation.)

3 Likes