Make string from characters

Hi everyone, why is the following not true (“ab” == str(‘a’) + str(‘b’)) and how can I adjust it so that it will be true? See the example below:

void setup() {
  size(200, 200);
}

void draw() {
    println("ab", str('a') + str('b'), "ab" == str('a') + str('b'));
    exit();
}

Thanks,
Q

1 Like
void setup() {
  size(200, 200);
}

void draw() {
    println("ab", str('a') + str('b'), ( "ab".equals(str('a') + str('b') ) ) );
    exit();
}

console: ab ab true

https://processing.org/reference/String_equals_.html

1 Like

https://processing.org/reference/String_equals_.html

:slight_smile:

Just to underscore the above links and examples, calling "mystring".equals("mystring") rather than "mystring"=="mystring" is necessary because, as the reference states:

it’s not possible to compare strings using the equality operator (==). Returns true if the strings are the same and false if they are not. equals() / Reference / Processing.org

This is inherited from Java, in which Strings are immutable objects and the == operator compares their memory addresses rather than their contents. The result is really unpleasant – depending on your code, you might accidentally get == to work on strings, but not for the reasons you think. Just don’t do it.

4 Likes

Just adding for future references: Troubleshooting: why-dont-these-strings-equal.

Kf

1 Like