Java String Equal areEqualIgnoreNull(String value1, String value2, boolean caseSensitive)

Here you can find the source of areEqualIgnoreNull(String value1, String value2, boolean caseSensitive)

Description

Compares the given values and returns true if they are equal.

License

Open Source License

Parameter

Parameter Description
value1 The first value to compare. Is ignored if null or empty String
value2 The second value to be compared

Return

true if both values are equal or if the first value is null or empty string.

Declaration

public static final boolean areEqualIgnoreNull(String value1, String value2, boolean caseSensitive) 

Method Source Code

//package com.java2s;
/*/*from ww w.j a  v a 2 s.c om*/
 *
 * The DbUnit Database Testing Framework
 * Copyright (C)2005, DbUnit.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

public class Main {
    /**
     * Compares the given values and returns true if they are equal.
     * If the first value is <code>null</code> or empty String it always
     * returns <code>true</code> which is the way of ignoring <code>null</code>s
     * for this specific case.
     * @param value1 The first value to compare. Is ignored if null or empty String
     * @param value2 The second value to be compared
     * @return <code>true</code> if both values are equal or if the first value
     * is <code>null</code> or empty string.
     * @since 2.4.4
     */
    public static final boolean areEqualIgnoreNull(String value1, String value2, boolean caseSensitive) {
        if (value1 == null || value1.equals("")) {
            return true;
        } else {
            if (caseSensitive && value1.equals(value2)) {
                return true;
            } else if (!caseSensitive && value1.equalsIgnoreCase(value2)) {
                return true;
            } else {
                return false;
            }
        }
    }
}

Related

  1. areEqual(String string1, String string2)
  2. areEqual(String string1, String string2)
  3. areEqual(String thisString, String thatString)
  4. areEqualConstantTime(String a, String b)
  5. areEqualIgnoreCase(String s1, String s2)
  6. areEqualLexemes(String l1, String l2)
  7. areEquals(final String origin, final String checkWith)
  8. areEquals(final String s1, final String s2)
  9. areEqualsIgnoreCaseAndTrim(final String s1, final String s2)