Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Comparing Strings  RSS feed

 
m avalla
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having a problem comparing strings. I have defined a String fieldtest1 and it is assigned a value. I then am populating a drop down box by processing a resultset (rs) and doing a rs.getString. Then I want to compare the 2 values by issuing the following:
if rs.getString.equals(fieldtest1)
{
...
}
If fieldtest1 has the value of "FOO" in it the if works, however if the value is "FOO FOO" the if does not work. Anytime fieldtest1 has a value where there is a space somewhere, the if will not work.
Can someone please help?
Thanks!
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not really a JSP question, but I'll give it a shot anyway.
The String comparison (equals) method will only be true if the Strings are exactly the same, character by character. I suspect that your Strings, are different in some way that you don't notice. For example, there might be white space (spaces or tabs, or even new lines) at the end of one of them. Also, the "space" in between the words might be two spaces, or a tab.
You could try the following code to see what's going on:

Try the same on fieldtest1. That should show you where the differences are.
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it all depends on what kind of behavior you're trying to get.... but I'm sure you'll be able to find the correct behavior in String
String.endsWith()
String.startsWith()
String.equalsIgnoreCase()
String.indexOf()
 
m avalla
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help....it turned out to be developer error.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!