If a string contains a char - Java java.lang

Java examples for java.lang:char

Description

If a string contains a char

Demo Code

public class Main {

  public static void main(String[] argv) {
    String str = "java2s.com";
    char searchChar = 'a';
    System.out.println(contains(str, searchChar));
  }// www. j a  v  a2 s  . c  om

  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;
  }

  public static int indexOf(String str, char searchChar) {
    if (isEmpty(str)) {
      return -1;
    }
    return str.indexOf(searchChar);
  }

}

Related Tutorials