Java String Equal areEqual(String string1, String string2)

Here you can find the source of areEqual(String string1, String string2)

Description

Checks two string instances for equality, allowing for null in either or both strings.

License

Apache License

Parameter

Parameter Description
string1 a String instance to compare. May be null.
string2 a String instance to compare. May be null.

Return

true if the String instances are lexically equal. Otherwise false.

Declaration

public static boolean areEqual(String string1, String string2) 

Method Source Code

//package com.java2s;
/*//from   w w  w  .  j  a  v a 2 s. c  om
   Copyright 2004-2008 Strategic Gains, Inc.
       
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
       
  http://www.apache.org/licenses/LICENSE-2.0
       
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

public class Main {
    /**
     * Checks two string instances for equality, allowing for null in either or
     * both strings. If both strings are null, they are considered equal.
     * 
     * @param string1 a String instance to compare. May be null.
     * @param string2 a String instance to compare. May be null.
     * @return true if the String instances are lexically equal. Otherwise
     *         false.
     * @see compareTo(String, String)
     */
    public static boolean areEqual(String string1, String string2) {
        return (compareTo(string1, string2) == 0);
    }

    /**
     * Compares two String instances, allowing null in either or both strings.
     * If both strings are null, they are considered equal. If only one string
     * is null, the non-null string is considered greater. If both strings are
     * non-null, this method returns the results of string1.compareTo(string2).
     * 
     * @param string1 a String instance to compare. May be null.
     * @param string2 a String instance to compare. May be null.
     * @return <0 if string1 < string2, 0 if string1.equals(string2), >0 if
     *         string1 > string2.
     */
    public static int compareTo(String string1, String string2) {
        int result = 0;

        if (string1 != null && string2 == null)
            result = 1;
        else if (string1 == null && string2 != null)
            result = -1;
        else if (string1 != null && string2 != null)
            result = string1.compareTo(string2);

        return result;
    }
}

Related

  1. areEqual(String s1, String s2)
  2. areEqual(String s1, String s2)
  3. areEqual(String seq1, String seq2)
  4. areEqual(String str1, String str2)
  5. areEqual(String string1, String string2)
  6. areEqual(String thisString, String thatString)
  7. areEqualConstantTime(String a, String b)
  8. areEqualIgnoreCase(String s1, String s2)
  9. areEqualIgnoreNull(String value1, String value2, boolean caseSensitive)