Alex Rivera | Logout

How do I make my string comparison case-insensitive?

Asked 2010-02-08T08:50:13.637
122

I created a Java program to compare two strings:

String str = "Hello";

if (str.equals("hello")) {
    System.out.println("match");
} else {
    System.out.println("no match");
}

It's case-sensitive. How can I change it so that it's not?

Edit
Report

2 Answers

24

You have to use the compareToIgnoreCase method of the String object.

int compareValue = str1.compareToIgnoreCase(str2);

if (compareValue == 0) it means str1 equals str2.

answered 2012-05-01T05:09:56.387
1

More about string can be found in String Class and String Tutorials

answered 2010-02-08T08:57:05.343

Your Answer