/** * Finds the very first index of a substring from the specified array. It * returns an int[2] where int[0] represents the substring index and int[1] * represents position where substring was found. Returns <code>null</code> * if noting found. * * @param s source string * @param arr string array */ public static int[] indexOfIgnoreCase(String s, String[] arr) { return indexOfIgnoreCase(s, arr, 0); } /**
/** * Finds the very first index of a substring from the specified array. It * returns an int[2] where int[0] represents the substring index and int[1] * represents position where substring was found. Returns <code>null</code> * if noting found. * * @param s source string * @param arr string array */ public static int[] indexOfIgnoreCase(final String s, final String... arr) { return indexOfIgnoreCase(s, arr, 0); } /**
/** * Finds the very first index of a substring from the specified array. It * returns an int[2] where int[0] represents the substring index and int[1] * represents position where substring was found. Returns <code>null</code> * if noting found. * * @param s source string * @param arr string array * @param start starting position */ public static int[] indexOfIgnoreCase(String s, String[] arr, int start) { int arrLen = arr.length; int index = Integer.MAX_VALUE; int last = -1; for (int j = 0; j < arrLen; j++) { int i = indexOfIgnoreCase(s, arr[j], start); if (i != -1) { if (i < index) { index = i; last = j; } } } return last == -1 ? null : new int[] {last, index}; }
/** * Finds first index of a substring in the given source string with ignored case. * * @param src source string for examination * @param subS substring to find * * @return index of founded substring or -1 if substring is not found * @see #indexOfIgnoreCase(String, String, int) */ public static int indexOfIgnoreCase(String src, String subS) { return indexOfIgnoreCase(src, subS, 0, src.length()); }
/** * Finds first index of a substring in the given source string with ignored case. * * @param src source string for examination * @param subS substring to find * * @return index of founded substring or -1 if substring is not found * @see #indexOfIgnoreCase(String, String, int) */ public static int indexOfIgnoreCase(final String src, final String subS) { return indexOfIgnoreCase(src, subS, 0, src.length()); }
/** * Finds first index of a substring in the given source string with ignored * case. This seems to be the fastest way doing this, with common string * length and content (of course, with no use of Boyer-Mayer type of * algorithms). Other implementations are slower: getting char array first, * lower casing the source string, using String.regionMatch etc. * * @param src source string for examination * @param subS substring to find * @param startIndex starting index from where search begins * * @return index of founded substring or -1 if substring is not found */ public static int indexOfIgnoreCase(String src, String subS, int startIndex) { return indexOfIgnoreCase(src, subS, startIndex, src.length()); } /**
/** * Removes the first 'select' from the sql query. */ protected String removeSelect(String sql) { int ndx = StringUtil.indexOfIgnoreCase(sql, "select"); if (ndx != -1) { sql = sql.substring(ndx + 6); // select.length() } return sql; }
/** * Finds the very first index of a substring from the specified array. It * returns an int[2] where int[0] represents the substring index and int[1] * represents position where substring was found. Returns <code>null</code> * if noting found. * * @param s source string * @param arr string array * @param start starting position */ public static int[] indexOfIgnoreCase(final String s, final String[] arr, final int start) { int arrLen = arr.length; int index = Integer.MAX_VALUE; int last = -1; for (int j = 0; j < arrLen; j++) { int i = indexOfIgnoreCase(s, arr[j], start); if (i != -1) { if (i < index) { index = i; last = j; } } } return last == -1 ? null : new int[] {last, index}; }
/** * Count substring occurrences in a source string, ignoring case. * * @param source source string * @param sub substring to count * @return number of substring occurrences */ public static int countIgnoreCase(String source, String sub) { int count = 0; int j = 0; int sublen = sub.length(); if (sublen == 0) { return 0; } while (true) { int i = indexOfIgnoreCase(source, sub, j); if (i == -1) { break; } count++; j = i + sublen; } return count; }
/** * Finds first index of a substring in the given source string with ignored * case. This seems to be the fastest way doing this, with common string * length and content (of course, with no use of Boyer-Mayer type of * algorithms). Other implementations are slower: getting char array first, * lower casing the source string, using String.regionMatch etc. * * @param src source string for examination * @param subS substring to find * @param startIndex starting index from where search begins * * @return index of founded substring or -1 if substring is not found */ public static int indexOfIgnoreCase(final String src, final String subS, final int startIndex) { return indexOfIgnoreCase(src, subS, startIndex, src.length()); } /**
public static boolean validate(final Object value, final String substring, final boolean ignoreCase) { if (value == null) { return true; } if (ignoreCase) { return StringUtil.indexOfIgnoreCase(value.toString(), substring) > -1; } return value.toString().contains(substring); }
/** * Count substring occurrences in a source string, ignoring case. * * @param source source string * @param sub substring to count * @return number of substring occurrences */ public static int countIgnoreCase(final String source, final String sub) { int count = 0; int j = 0; int sublen = sub.length(); if (sublen == 0) { return 0; } while (true) { int i = indexOfIgnoreCase(source, sub, j); if (i == -1) { break; } count++; j = i + sublen; } return count; }
/** * Replaces many substring at once. Order of string array is important. * * @param s source string * @param sub substrings array * @param with replace with array * * @return string with all occurrences of substrings replaced */ public static String replaceIgnoreCase(String s, String[] sub, String[] with) { if ((sub.length != with.length) || (sub.length == 0)) { return s; } int start = 0; StringBuilder buf = new StringBuilder(s.length()); while (true) { int[] res = indexOfIgnoreCase(s, sub, start); if (res == null) { break; } int end = res[1]; buf.append(s.substring(start, end)); buf.append(with[res[0]]); start = end + sub[0].length(); } buf.append(s.substring(start)); return buf.toString(); }
/** * Replaces many substring at once. Order of string array is important. * * @param s source string * @param sub substrings array * @param with replace with array * * @return string with all occurrences of substrings replaced */ public static String replaceIgnoreCase(final String s, final String[] sub, final String[] with) { if ((sub.length != with.length) || (sub.length == 0)) { return s; } int start = 0; StringBuilder buf = new StringBuilder(s.length()); while (true) { int[] res = indexOfIgnoreCase(s, sub, start); if (res == null) { break; } int end = res[1]; buf.append(s, start, end); buf.append(with[res[0]]); start = end + sub[0].length(); } buf.append(s.substring(start)); return buf.toString(); }
/** * Removes the first part of the sql up to the relevant 'from'. * Tries to detect sub-queries in the 'select' part. */ protected String removeToFrom(String sql) { int from = 0; int fromCount = 1; int selectCount = 0; int lastNdx = 0; while (true) { int ndx = StringUtil.indexOfIgnoreCase(sql, "from", from); if (ndx == -1) { break; } // count selects in left part String left = sql.substring(lastNdx, ndx); selectCount += StringUtil.countIgnoreCase(left, "select"); if (fromCount >= selectCount) { sql = sql.substring(ndx); break; } // find next 'from' lastNdx = ndx; from = ndx + 4; fromCount++; } return sql; }
assertEquals(1, StringUtil.indexOfIgnoreCase(src, "345", "234")[0]); assertEquals(1, StringUtil.indexOfIgnoreCase(src, "345", "234")[1]); assertEquals(0, StringUtil.lastIndexOfIgnoreCase(src, "345", "234")[0]); assertEquals(2, StringUtil.lastIndexOfIgnoreCase(src, "345", "234")[1]); assertEquals(10, StringUtil.indexOfIgnoreCase(src, 'Q', 5, 20)); assertEquals(-1, StringUtil.lastIndexOf("123", "12345", 0, 5)); assertEquals(-1, StringUtil.lastIndexOf("", 'a', 0, 5)); assertEquals(0, StringUtil.indexOfIgnoreCase(src, "123")); assertEquals(0, StringUtil.lastIndexOfIgnoreCase(src, "123")); assertEquals(0, src.lastIndexOf("123")); assertEquals(0, StringUtil.indexOfIgnoreCase(src, "1234567890QwErTy")); assertEquals(0, StringUtil.lastIndexOfIgnoreCase(src, "1234567890QwErTy")); assertTrue(StringUtil.startsWithIgnoreCase(src, "1234567890QwErTy")); assertTrue(StringUtil.endsWithIgnoreCase(src, "1234567890QwErTy")); assertEquals(1, StringUtil.indexOfIgnoreCase(src, "2345")); assertEquals(1, StringUtil.lastIndexOfIgnoreCase(src, "2345")); assertFalse(StringUtil.startsWithIgnoreCase(src, "2345")); assertEquals(10, StringUtil.indexOfIgnoreCase(src, "qwe")); assertEquals(10, StringUtil.lastIndexOfIgnoreCase(src, "qwe")); assertTrue(StringUtil.startsWithIgnoreCase(src, "qwe", 10)); assertEquals(10, StringUtil.indexOfIgnoreCase(src, "qwerty")); assertEquals(10, StringUtil.lastIndexOfIgnoreCase(src, "qwerty")); assertTrue(StringUtil.startsWithIgnoreCase(src, "qwerty", 10));
assertEquals(1, StringUtil.indexOfIgnoreCase(s, "wEr", 0, 5)); assertEquals(1, StringUtil.indexOfIgnoreCase(s, "wEr", 1, 5)); assertEquals(-1, StringUtil.indexOfIgnoreCase(s, "wEr", 2, 5)); assertEquals(1, StringUtil.indexOfIgnoreCase(s, "wEr", 1, 4)); assertEquals(-1, StringUtil.indexOfIgnoreCase(s, "wEr", 0, 3)); assertEquals(6, StringUtil.indexOfIgnoreCase(s, "iOp", 0, s.length())); assertEquals(6, StringUtil.indexOfIgnoreCase(s, "iOp", 6, s.length())); assertEquals(-1, StringUtil.indexOfIgnoreCase(s, "iOp", 0, s.length() - 1)); assertEquals(-1, StringUtil.indexOfIgnoreCase(s, "iOp", 7, s.length()));
@Test void testArrays() { String s = "qwertyuiop"; assertEquals("qWERtyuIOp", StringUtil.replace(s, new String[]{"wer", "io"}, new String[]{"WER", "IO"})); assertEquals("qwertyuiop", StringUtil.replace(s, new String[]{"wer1", "io1"}, new String[]{"WER", "IO"})); assertEquals("qWERtyuIOP", StringUtil.replace(s, new String[] {"wer", "iop"}, new String[] {"WER", "IOP"})); assertEquals("qWERtyuIOP", StringUtil.replaceIgnoreCase(s, new String[] {"WER", "IOP"}, new String[] {"WER", "IOP"})); assertEquals(s, StringUtil.replaceIgnoreCase(s, new String[] {"WER", "IOP"}, new String[] {"WER"})); assertEquals(s, StringUtil.replaceIgnoreCase(s, new String[] {}, new String[] {"WER"})); assertEquals("qwertyuiop", StringUtil.replace(s, new String[] {}, new String[] {})); assertEquals("qWERtyuiop", StringUtil.replace(s, new String[] {"wer", "we"}, new String[] {"WER", "11"})); assertTrue(StringUtil.equals(new String[] {"wer", "io"}, new String[] {"wer", "io"})); assertFalse(StringUtil.equals(new String[] {"wer", "io"}, new String[] {"WER", "IO"})); assertTrue(StringUtil.equalsIgnoreCase(new String[] {"wer", "io"}, new String[] {"WER", "IO"})); assertEquals(1, StringUtil.indexOf(s, "wer", "io")[1]); assertEquals(7, StringUtil.indexOfIgnoreCase(s, new String[]{"wer", "IO"}, 2)[1]); assertEquals(7, StringUtil.lastIndexOf(s, "wer", "io")[1]); assertEquals(1, StringUtil.lastIndexOfIgnoreCase(s, new String[]{"WER", "io"}, 5)[1]); }
/** * Finds the very first index of a substring from the specified array. It * returns an int[2] where int[0] represents the substring index and int[1] * represents position where substring was found. Returns <code>null</code> * if noting found. * * @param s source string * @param arr string array */ public static int[] indexOfIgnoreCase(String s, String arr[]) { return indexOfIgnoreCase(s, arr, 0); }
public static boolean validate(Object value, String substring, boolean ignoreCase) { if (value == null) { return true; } if (ignoreCase) { return StringUtil.indexOfIgnoreCase(value.toString(), substring) > -1; } return value.toString().indexOf(substring) > -1; }