Compares two strings properly, even when one of them is null - without throwing up - Android java.lang

Android examples for java.lang:String Compare

Description

Compares two strings properly, even when one of them is null - without throwing up

Demo Code

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Main{

    /**/* ww w  . j av a 2 s . c o m*/
     * Compares two strings properly, even when one of them is null - without throwing up
     *
     * @param str1 The first string
     * @param str2 Guess?
     * @return true if they are both equal (even if both are null)
     */
    public static boolean compareStrings(@Nullable String str1,
            @Nullable String str2) {
        return (str1 == null ? str2 == null : str1.equals(str2));
    }

}

Related Tutorials