/** * Returns the index of the first occurrence of <code>subtext</code> within * <code>text</code>; or <code>-1</code> if <code>subtext</code> does not * occur within <code>text</code>. * * @param text String in which to locate <code>subtext</code> * @return the index of the first occurrence of <code>subtext</code> within * <code>text</code>; or <code>-1</code> * @throws IllegalStateException if no subtext has been set */ public int indexOf(String text) { if (pattern == null) throw new IllegalStateException("setSubtext must be called with a valid value before this method can operate"); if (text.length() == 0) return -1; final int index = new StringSearch(pattern, new StringCharacterIterator(text), COLLATOR).first(); return mode == TextMatcherEditor.STARTS_WITH && index != 0 ? -1 : index; } }
/** * Returns the index of the first occurrence of <code>subtext</code> within * <code>text</code>; or <code>-1</code> if <code>subtext</code> does not * occur within <code>text</code>. * * @param text String in which to locate <code>subtext</code> * @return the index of the first occurrence of <code>subtext</code> within * <code>text</code>; or <code>-1</code> * @throws IllegalStateException if no subtext has been set */ public int indexOf(String text) { if (pattern == null) throw new IllegalStateException("setSubtext must be called with a valid value before this method can operate"); if (text.length() == 0) return -1; final int index = new StringSearch(pattern, new StringCharacterIterator(text), COLLATOR).first(); return mode == TextMatcherEditor.STARTS_WITH && index != 0 ? -1 : index; } }
/** * Returns the index of the first occurrence of <code>subtext</code> within * <code>text</code>; or <code>-1</code> if <code>subtext</code> does not * occur within <code>text</code>. * * @param text String in which to locate <code>subtext</code> * @return the index of the first occurrence of <code>subtext</code> within * <code>text</code>; or <code>-1</code> * @throws IllegalStateException if no subtext has been set */ @Override public int indexOf(String text) { if (pattern == null) { throw new IllegalStateException("setSubtext must be called with a valid value before this method can operate"); } if (text.length() == 0) { return -1; } final int index = new StringSearch(pattern, new StringCharacterIterator(text), COLLATOR).first(); return mode == TextMatcherEditor.STARTS_WITH && index != 0 ? -1 : index; } }
public static int indexOfIgnoreCase(String haystack, String needle) { StringSearch stringSearch = new StringSearch(needle, haystack); stringSearch.getCollator().setStrength(Collator.PRIMARY); return stringSearch.first(); } }
public static boolean containsIgnoreCase(String haystack, String needle) { return indexOfIgnoreCase(haystack, needle) >= 0; } public static int indexOfIgnoreCase(String haystack, String needle) { StringSearch stringSearch = new StringSearch(needle, haystack); stringSearch.getCollator().setStrength(Collator.PRIMARY); return stringSearch.first(); }