Write code to check the index Of Any char in the char array among a string
//package com.book2s; public class Main { public static void main(String[] argv) { String str = "book2s.com"; char[] searchChars = new char[] { 'b', 'o', 'o', 'k', '2', 's', '.', 'c', 'o', 'm', 'a', '1', }; System.out.println(indexOfAny(str, searchChars)); }//from ww w. j a v a 2 s . com public static int indexOfAny(String str, char[] searchChars) { if ((str == null) || (str.length() == 0) || (searchChars == null) || (searchChars.length == 0)) { return -1; } for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); for (int j = 0; j < searchChars.length; j++) { if (searchChars[j] == ch) { return i; } } } return -1; } public static int indexOfAny(String str, String searchChars) { if ((str == null) || (str.length() == 0) || (searchChars == null) || (searchChars.length() == 0)) { return -1; } for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); for (int j = 0; j < searchChars.length(); j++) { if (searchChars.charAt(j) == ch) { return i; } } } return -1; } public static int indexOfAny(String str, String[] searchStrs) { if ((str == null) || (searchStrs == null)) { return -1; } int sz = searchStrs.length; // String's can't have a MAX_VALUEth index. int ret = Integer.MAX_VALUE; int tmp = 0; for (int i = 0; i < sz; i++) { String search = searchStrs[i]; if (search == null) { continue; } tmp = str.indexOf(search); if (tmp == -1) { continue; } if (tmp < ret) { ret = tmp; } } return (ret == Integer.MAX_VALUE) ? (-1) : ret; } }