Use String.trim()
to remove surrounding whitespace and then use String.equals()
(not ==
, See 15.21 Equality Operators in the Java Language Specification for full details.). Remember that String
instances are immutable so String.trim()
returns a new String
and it is that which must be used in the equals()
check.
Note that trim()
removes leading whitespace also. If this is undesired then use String.substring()
to erase the trailing whitespace.
solved Java string end with space