0

Screencap

I am comparing two strings, and apparently they aren't equal but they are. I was wondering what are some issues that could cause this, maybe datatype, hidden stuff that I can't see, or what. Any input could be helpful. Thanks.

Jackson Bray
  • 235
  • 2
  • 7
  • 17
  • You're **not** doing value comparison; the `==` operator when applied to objects does *reference* comparison - it will only return true if they're the exact same object. – Anthony Grist Jan 17 '13 at 16:10

4 Answers4

2

String/Object comparison should use equals(), not == (excepts the case of String literal comparison)

Example:

if(string.equals(string2)){
}

== checks for reference equality, equals() checks for content equality. Read this discussion.

Community
  • 1
  • 1
kosa
  • 65,990
  • 13
  • 130
  • 167
2
field.trim().equalsIgnoreCase("formDateCreated")

Try this. String trim() Method returns a copy of the string, with leading and trailing whitespace omitted.

Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
1

use .equals() method to check for String equality like field1.equals("formDateCreated"). == operator just checks if two reference variables refer to the same string instance. equals() method checks if two strings are meaningfully equal.

someone
  • 6,577
  • 7
  • 37
  • 60
PermGenError
  • 45,977
  • 8
  • 87
  • 106
1
equalsIgnoreCase(String anotherString)

for when you are not consider about case. This check weather your string content is equal not the reference which is ==

I would suggest you to check like bellow

"formDateCreated".equalsIgnoreCase(field)

So that it helps you to overcome possible NullPointerException

someone
  • 6,577
  • 7
  • 37
  • 60