Compares two Strings, returning true if they are equal ignoring the case. - Java java.lang

Java examples for java.lang:String Compare

Description

Compares two Strings, returning true if they are equal ignoring the case.

Demo Code

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String str1 = "java2s.com";
        String str2 = "java2s.com";
        System.out.println(equalsIgnoreCase(str1, str2));
    }/* www  . j  a  v  a 2  s.co  m*/

    /**
     * <p>
     * Compares two Strings, returning <code>true</code> if they are equal
     * ignoring the case.
     * </p>
     * 
     * <p>
     * <code>null</code>s are handled without exceptions. Two <code>null</code>
     * references are considered equal. Comparison is case insensitive.
     * </p>
     * 
     * <pre>
     * StringUtils.equalsIgnoreCase(null, null)   = true
     * StringUtils.equalsIgnoreCase(null, "abc")  = false
     * StringUtils.equalsIgnoreCase("abc", null)  = false
     * StringUtils.equalsIgnoreCase("abc", "abc") = true
     * StringUtils.equalsIgnoreCase("abc", "ABC") = true
     * </pre>
     * 
     * @param str1
     *            the first String, may be null
     * @param str2
     *            the second String, may be null
     * @return <code>true</code> if the Strings are equal, case insensitive, or
     *         both <code>null</code>
     * @see java.lang.String#equalsIgnoreCase(String)
     */
    public static boolean equalsIgnoreCase(final String str1,
            final String str2) {
        return str1 == null ? str2 == null : str1.equalsIgnoreCase(str2);
    }
}

Related Tutorials