class cLastIndexOfAny { static int LastIndexOfAny(char[] cSearch, String sTarget) { if (sTarget == null || sTarget.length() == 0 || cSearch == null || cSearch.length == 0) { return -1; } for (int i = sTarget.length(); i >= 0; i --) { char ch = sTarget.charAt(i - 1); for (int j = 0; j < cSearch.length; j++) { if (cSearch[j] == ch) { return i - 1; } } } return -1; } static int LastIndexOfAny(char[] cSearch, int iStart, String sTarget) { if (sTarget == null || sTarget.length() == 0 || cSearch == null || cSearch.length == 0) { return -1; } for (int i = sTarget.length(); i > iStart; i --) { char ch = sTarget.charAt(i - 1); for (int j = 0; j < cSearch.length; j++) { if (cSearch[j] == ch) { return i - 1; } } } return -1; } static int LastIndexOfAny(char[] cSearch, int iStart, int iEnd, String sTarget) { if (sTarget == null || sTarget.length() == 0 || cSearch == null || cSearch.length == 0) { return -1; } for(int i = sTarget.substring(iStart, iStart + iEnd).length(); i >= 0; i--) { char ch = sTarget.substring(iStart, iStart + iEnd).charAt(i - 1); for (int j = 0; j < cSearch.length; j++) { if (cSearch[j] == ch) { return iStart + i - 1; } } } return -1; } } public class StringLastIndexOfAny { public StringLastIndexOfAny() { // 必要な変数を宣言する String stTarget = "EDCBABCDE"; char[] chFinds = {'B', 'C', 'D'}; // 末尾から 'B', 'C', 'D' のいずれかを検索し、見つかった位置を取得する int iFind1 = cLastIndexOfAny.LastIndexOfAny(chFinds, stTarget); // 3 文字目の後から 'B', 'C', 'D' のいずれかを検索し、見つかった位置を取得する int iFind2 = cLastIndexOfAny.LastIndexOfAny(chFinds, 3, stTarget); // 4 文字目の後から 2 文字まで 'B', 'C', 'D' のいずれかを検索し、見つかった位置を取得する int iFind3 = cLastIndexOfAny.LastIndexOfAny(chFinds, 4, 2, stTarget); // すべての結果を表示する System.out.println("iFind1 = " + Integer.toString(iFind1)); System.out.println("iFind2 = " + Integer.toString(iFind2)); System.out.println("iFind3 = " + Integer.toString(iFind3)); } public static void main(String[] args) { StringLastIndexOfAny stringLastIndexOfAny = new StringLastIndexOfAny(); } }