1

Both strings appear to be the same when printed to the console, but not when compared using "=="

What am i doing wrong here?

String message = "Rejected | Ref ID: CaptureMe | Name:";  

Pattern pattern = Pattern.compile("\\bRef ID:\\s+(\\S+)");     

Matcher matcher = pattern.matcher(message);

String matchedRef = matcher.group(1); 
System.out.print(matchedRef);    

Prints: CaptureMe

String myRef = "CaptureMe";

if(matchedRef == myRef){
System.out.print(true);
}
else{
System.out.print(false);
}

Prints: FALSE

HappyPoofySquirrel
  • 109
  • 1
  • 1
  • 7

1 Answers1

2

To compare strings you need to use the equals() method, not the == operator.

if(matchedRef.equals(myRef)){
    System.out.print(true);
}
else{
    System.out.print(false);
}

You can read more about String comparisons in this question.

Community
  • 1
  • 1
Chris Stillwell
  • 10,266
  • 10
  • 67
  • 77