Checks if String contains a search String, handling null
.
This method uses String#indexOf(String).
contains(null, *) = false contains(*, null) = false contains("", "") = true contains("abc", "") = true contains("abc", "a") = true contains("abc", "z") = false
public class Main { public static void main(String[] argv) throws Exception { String str = "demo2s.com"; String searchChar = "a"; System.out.println(contains(str, searchChar)); }//from w w w . j a v a2 s . c o m public static boolean contains(String str, String searchStr) { if (str == null || searchStr == null) { return false; } return str.indexOf(searchStr) >= 0; } public static boolean isEmpty(String str) { return str == null || str.length() == 0; } }