Java String contain a substring ignoring case

Description

Java String contain a substring ignoring case

public class Main {
  public static void main(String[] argv) throws Exception {
    String str = "demo2s.COM";
    String searchStr = ".com";
    System.out.println(containsIgnoreCase(str, searchStr));
  }/*  ww w.j a v a2  s  . c  o m*/

  /**
   * <p>
   * Checks if String contains a search String irrespective of case, handling
   * <code>null</code>. This method uses {@link #contains(String, String)}.
   * </p>
   *
   * <p>
   * A <code>null</code> String will return <code>false</code>.
   * </p>
   *
   * <pre>
   * contains(null, *) = false
   * contains(*, null) = false
   * contains("", "") = true
   * contains("abc", "") = true
   * contains("abc", "a") = true
   * contains("abc", "z") = false
   * contains("abc", "A") = true
   * contains("abc", "Z") = false
   * </pre>
   *
   * @param str
   *          the String to check, may be null
   * @param searchStr
   *          the String to find, may be null
   * @return true if the String contains the search String irrespective of case or
   *         false if not or <code>null</code> string input
   */
  public static boolean containsIgnoreCase(String str, String searchStr) {
    if (str == null || searchStr == null) {
      return false;
    }
    return contains(str.toUpperCase(), searchStr.toUpperCase());
  }
  public static boolean contains(String str, char searchChar) {
    if (isEmpty(str)) {
      return false;
    }
    return str.indexOf(searchChar) >= 0;
  }
  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;
  }

}



PreviousNext

Related