• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Comparing Strings

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the help....it turned out to be developer error.
 
reply
    Bookmark Topic Watch Topic
  • New Topic