Results 1 to 4 of 4
Thread: String compare issues?
- 04-21-2009, 05:27 AM #1
Member
- Join Date
- Apr 2009
- Posts
- 5
- Rep Power
- 0
String compare issues?
I am trying to compare two strings using the .equals() option. I am obtaining a value from a JDBC connection and trying to validate it against a user entered string, however it always fails. I have the following code -
String search = request.getParameter("usercn"); //get usercn from HTML form
while (rs.next()) {
String result = rs.getString(1); //get the current loop value
out.println("Current node:" + result + "<br>"); //print out current value
out.println("search:" + search + "<br>"); //print out user string
String r1 = new String (result);
String r2 = new String (search);
out.println("r1:" + r1 + "<br>"); // print out r1 value
out.println("r2:" + r2 + "<br>"); //print out r2 value
out.println(r1.equalsIgnoreCase(r2)); //debug - print out true/false
if (r1.equalsIgnoreCase(r2)){
String match = "true";
out.println("match" + r1);}
what I see is the following -
r1:root
r2:root
false
even though the strings appear the same, the comparison is always false.
Any thoughts??
- 04-21-2009, 06:46 AM #2
Hi,
One small suggestion.u trim that string value and try like this below
r1.trim().equalsIgnoreCase(r2.trim())
- 04-21-2009, 06:53 AM #3
String search ="";
if((request.getParameter("usercn") !=null) ||( request.getParameter("usercn").length() != 0) ||(request.getParameter("usercn") != ""))
{
search = request.getParameter("usercn");
}
while (rs.next()) {
//Here dont retreive via numbers.Readability sake retreive via columnName
String result = rs.getString(1); //get the current loop value
out.println("Current node:" + result + "<br>"); //print out current value
out.println("search:" + search + "<br>"); //print out user string
//The below lines are not needed.Already u are having string
//String r1 = new String (result);
//String r2 = new String (search);
out.println(result.trim().equalsIgnoreCase(search. trim())); //debug - print out true/false
if (r1.equalsIgnoreCase(r2)){
String match = "true";
out.println("match" + r1);}
- 04-21-2009, 07:16 AM #4
Member
- Join Date
- Apr 2009
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Comparing string using == or != (how to compare string in if else)
By fiqueudrue in forum New To JavaReplies: 6Last Post: 02-10-2009, 06:48 AM -
Compare 2 XML
By Peter in forum XMLReplies: 1Last Post: 07-05-2007, 02:58 AM -
String Compare not working
By Revelation in forum New To JavaReplies: 3Last Post: 06-30-2007, 06:43 PM -
compare speed
By bbq in forum JDBCReplies: 1Last Post: 06-28-2007, 05:34 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks