/** * Checks if the string builder contains the specified string. * * @param str the string to find * @return true if the builder contains the string */ public boolean contains(final String str) { return indexOf(str, 0) >= 0; }
/** * Searches the string builder to find the first reference to the specified char. * * @param ch the character to find * @return the first index of the character, or -1 if not found */ public int indexOf(final char ch) { return indexOf(ch, 0); }
/** * Searches the string builder to find the first reference to the specified string. * <p> * Note that a null input string will return -1, whereas the JDK throws an exception. * * @param str the string to find, null returns -1 * @return the first index of the string, or -1 if not found */ public int indexOf(final String str) { return indexOf(str, 0); }
/** * Checks if the string builder contains a string matched using the * specified matcher. * <p> * Matchers can be used to perform advanced searching behaviour. * For example you could write a matcher to search for the character * 'a' followed by a number. * * @param matcher the matcher to use, null returns -1 * @return true if the matcher finds a match in the builder */ public boolean contains(final StrMatcher matcher) { return indexOf(matcher, 0) >= 0; }
/** * Searches the string builder using the matcher to find the first match. * <p> * Matchers can be used to perform advanced searching behaviour. * For example you could write a matcher to find the character 'a' * followed by a number. * * @param matcher the matcher to use, null returns -1 * @return the first index matched, or -1 if not found */ public int indexOf(final StrMatcher matcher) { return indexOf(matcher, 0); }
/** * Deletes the string wherever it occurs in the builder. * * @param str the string to delete, null causes no action * @return this, to enable chaining */ public StrBuilder deleteAll(final String str) { final int len = (str == null ? 0 : str.length()); if (len > 0) { int index = indexOf(str, 0); while (index >= 0) { deleteImpl(index, index + len, len); index = indexOf(str, index); } } return this; }
/** * Replaces the search string with the replace string throughout the builder. * * @param searchStr the search string, null causes no action to occur * @param replaceStr the replace string, null is equivalent to an empty string * @return this, to enable chaining */ public StrBuilder replaceAll(final String searchStr, final String replaceStr) { final int searchLen = (searchStr == null ? 0 : searchStr.length()); if (searchLen > 0) { final int replaceLen = (replaceStr == null ? 0 : replaceStr.length()); int index = indexOf(searchStr, 0); while (index >= 0) { replaceImpl(index, index + searchLen, searchLen, replaceStr, replaceLen); index = indexOf(searchStr, index + replaceLen); } } return this; }
/** * Deletes the string wherever it occurs in the builder. * * @param str the string to delete, null causes no action * @return this, to enable chaining */ public StrBuilder deleteFirst(final String str) { final int len = (str == null ? 0 : str.length()); if (len > 0) { final int index = indexOf(str, 0); if (index >= 0) { deleteImpl(index, index + len, len); } } return this; }
/** * Replaces the first instance of the search string with the replace string. * * @param searchStr the search string, null causes no action to occur * @param replaceStr the replace string, null is equivalent to an empty string * @return this, to enable chaining */ public StrBuilder replaceFirst(final String searchStr, final String replaceStr) { final int searchLen = (searchStr == null ? 0 : searchStr.length()); if (searchLen > 0) { final int index = indexOf(searchStr, 0); if (index >= 0) { final int replaceLen = (replaceStr == null ? 0 : replaceStr.length()); replaceImpl(index, index + searchLen, searchLen, replaceStr, replaceLen); } } return this; }
return indexOf(str.charAt(0), startIndex);
@Test public void testIndexOf_char() { final StrBuilder sb = new StrBuilder("abab"); assertEquals(0, sb.indexOf('a')); // should work like String#indexOf assertEquals("abab".indexOf('a'), sb.indexOf('a')); assertEquals(1, sb.indexOf('b')); assertEquals("abab".indexOf('b'), sb.indexOf('b')); assertEquals(-1, sb.indexOf('z')); }
@Test public void testIndexOf_String() { final StrBuilder sb = new StrBuilder("abab"); assertEquals(0, sb.indexOf("a")); //should work like String#indexOf assertEquals("abab".indexOf("a"), sb.indexOf("a")); assertEquals(0, sb.indexOf("ab")); //should work like String#indexOf assertEquals("abab".indexOf("ab"), sb.indexOf("ab")); assertEquals(1, sb.indexOf("b")); assertEquals("abab".indexOf("b"), sb.indexOf("b")); assertEquals(1, sb.indexOf("ba")); assertEquals("abab".indexOf("ba"), sb.indexOf("ba")); assertEquals(-1, sb.indexOf("z")); assertEquals(-1, sb.indexOf((String) null)); }
@Test public void testIndexOf_char_int() { StrBuilder sb = new StrBuilder("abab"); assertEquals(0, sb.indexOf('a', -1)); assertEquals(0, sb.indexOf('a', 0)); assertEquals(2, sb.indexOf('a', 1)); assertEquals(-1, sb.indexOf('a', 4)); assertEquals(-1, sb.indexOf('a', 5)); // should work like String#indexOf assertEquals("abab".indexOf('a', 1), sb.indexOf('a', 1)); assertEquals(3, sb.indexOf('b', 2)); assertEquals("abab".indexOf('b', 2), sb.indexOf('b', 2)); assertEquals(-1, sb.indexOf('z', 2)); sb = new StrBuilder("xyzabc"); assertEquals(2, sb.indexOf('z', 0)); assertEquals(-1, sb.indexOf('z', 3)); }
@Test public void testIndexOf_String_int() { StrBuilder sb = new StrBuilder("abab"); assertEquals(0, sb.indexOf("a", -1)); assertEquals(0, sb.indexOf("a", 0)); assertEquals(2, sb.indexOf("a", 1)); assertEquals(2, sb.indexOf("a", 2)); assertEquals(-1, sb.indexOf("a", 3)); assertEquals(-1, sb.indexOf("a", 4)); assertEquals(-1, sb.indexOf("a", 5)); assertEquals(-1, sb.indexOf("abcdef", 0)); assertEquals(0, sb.indexOf("", 0)); assertEquals(1, sb.indexOf("", 1)); //should work like String#indexOf assertEquals ("abab".indexOf("a", 1), sb.indexOf("a", 1)); assertEquals(2, sb.indexOf("ab", 1)); //should work like String#indexOf assertEquals("abab".indexOf("ab", 1), sb.indexOf("ab", 1)); assertEquals(3, sb.indexOf("b", 2)); assertEquals("abab".indexOf("b", 2), sb.indexOf("b", 2)); assertEquals(1, sb.indexOf("ba", 1)); assertEquals("abab".indexOf("ba", 2), sb.indexOf("ba", 2)); assertEquals(-1, sb.indexOf("z", 2)); sb = new StrBuilder("xyzabc"); assertEquals(2, sb.indexOf("za", 0)); assertEquals(-1, sb.indexOf("za", 3)); assertEquals(-1, sb.indexOf((String) null, 2)); }
@Test public void testIndexOf_StrMatcher() { final StrBuilder sb = new StrBuilder(); assertEquals(-1, sb.indexOf((StrMatcher) null)); assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('a'))); sb.append("ab bd"); assertEquals(0, sb.indexOf(StrMatcher.charMatcher('a'))); assertEquals(1, sb.indexOf(StrMatcher.charMatcher('b'))); assertEquals(2, sb.indexOf(StrMatcher.spaceMatcher())); assertEquals(4, sb.indexOf(StrMatcher.charMatcher('d'))); assertEquals(-1, sb.indexOf(StrMatcher.noneMatcher())); assertEquals(-1, sb.indexOf((StrMatcher) null)); sb.append(" A1 junction"); assertEquals(6, sb.indexOf(A_NUMBER_MATCHER)); }
@Test public void testIndexOf_StrMatcher_int() { final StrBuilder sb = new StrBuilder(); assertEquals(-1, sb.indexOf((StrMatcher) null, 2)); assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('a'), 2)); assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('a'), 0)); assertEquals(0, sb.indexOf(StrMatcher.charMatcher('a'), -2)); assertEquals(0, sb.indexOf(StrMatcher.charMatcher('a'), 0)); assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('a'), 2)); assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('a'), 20)); assertEquals(1, sb.indexOf(StrMatcher.charMatcher('b'), -1)); assertEquals(1, sb.indexOf(StrMatcher.charMatcher('b'), 0)); assertEquals(1, sb.indexOf(StrMatcher.charMatcher('b'), 1)); assertEquals(3, sb.indexOf(StrMatcher.charMatcher('b'), 2)); assertEquals(3, sb.indexOf(StrMatcher.charMatcher('b'), 3)); assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('b'), 4)); assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('b'), 5)); assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('b'), 6)); assertEquals(2, sb.indexOf(StrMatcher.spaceMatcher(), -2)); assertEquals(2, sb.indexOf(StrMatcher.spaceMatcher(), 0)); assertEquals(2, sb.indexOf(StrMatcher.spaceMatcher(), 2)); assertEquals(-1, sb.indexOf(StrMatcher.spaceMatcher(), 4)); assertEquals(-1, sb.indexOf(StrMatcher.spaceMatcher(), 20)); assertEquals(-1, sb.indexOf(StrMatcher.noneMatcher(), 0)); assertEquals(-1, sb.indexOf((StrMatcher) null, 0));
@Test public void testIndexOfLang294() { final StrBuilder sb = new StrBuilder("onetwothree"); sb.deleteFirst("three"); assertEquals(-1, sb.indexOf("three")); }
@Test public void testLang295() { final StrBuilder sb = new StrBuilder("onetwothree"); sb.deleteFirst("three"); assertFalse( "The contains(char) method is looking beyond the end of the string", sb.contains('h')); assertEquals( "The indexOf(char) method is looking beyond the end of the string", -1, sb.indexOf('h')); }
/** * Searches the string builder to find the first reference to the specified string. * <p> * Note that a null input string will return -1, whereas the JDK throws an exception. * * @param str the string to find, null returns -1 * @return the first index of the string, or -1 if not found */ public int indexOf(final String str) { return indexOf(str, 0); }
/** * Searches the string builder using the matcher to find the first match. * <p> * Matchers can be used to perform advanced searching behaviour. * For example you could write a matcher to find the character 'a' * followed by a number. * * @param matcher the matcher to use, null returns -1 * @return the first index matched, or -1 if not found */ public int indexOf(final StrMatcher matcher) { return indexOf(matcher, 0); }