If String contains another string and handle null value - Android java.lang

Android examples for java.lang:String Contains

Description

If String contains another string and handle null value

Demo Code

public class Main{

    /**/*from  w  ww  . j av  a  2s .  c om*/
     * <pre>
     * StringUtil.contains(null, *) = false
     * StringUtil.contains(*, null) = false
     * StringUtil.contains("", "") = true
     * StringUtil.contains("abc", "") = true
     * StringUtil.contains("abc", "a") = true
     * StringUtil.contains("abc", "z") = false
     * </pre>
     * 
     */
    public static boolean contains(String str, String searchStr) {
        if ((str == null) || (searchStr == null)) {
            return false;
        }

        return str.indexOf(searchStr) >= 0;
    }

}

Related Tutorials