I have the following code:
String tmp = "cif";
String control = tmp.substring(1);
if(control == "if") {
append = "if( ";
}
However, despite control being "if", the test will still fail. Any solutions?
I have the following code:
String tmp = "cif";
String control = tmp.substring(1);
if(control == "if") {
append = "if( ";
}
However, despite control being "if", the test will still fail. Any solutions?
==
compares object references whereas .equals()
compares actual value
if(control.equals("if") {
append = "if( ";
}
String tmp = "cif";
String control = tmp.substring(1);
if(control.equals("if")) {
append = "if( ";
}
You cannot compare Strings via the == operator as Strings are objects.
Being objects, it does give the extra functionality of them having various methods inside of their classes. One such method which may come in handy is the equals() method.
The code you will therefore want would be:
String tmp = "cif";
String control = tmp.substring(1);
if("if".equals(control) {
...