Example usage for org.apache.commons.lang3 CharSequenceUtils indexOf

List of usage examples for org.apache.commons.lang3 CharSequenceUtils indexOf

Introduction

In this page you can find the example usage for org.apache.commons.lang3 CharSequenceUtils indexOf.

Prototype

static int indexOf(final CharSequence cs, final CharSequence searchChar, final int start) 

Source Link

Document

Used by the indexOf(CharSequence methods) as a green implementation of indexOf.

Usage

From source file:org.apache.commons.lang3.StringUtils.java

/**
 * <p>Finds the first index within a CharSequence, handling {@code null}.
 * This method uses {@link String#indexOf(int, int)} if possible.</p>
 *
 * <p>A {@code null} or empty ("") CharSequence will return {@code INDEX_NOT_FOUND (-1)}.</p>
 *
 * <pre>/* w  ww  .  j  a  v  a2  s. c o m*/
 * StringUtils.indexOf(null, *)         = -1
 * StringUtils.indexOf("", *)           = -1
 * StringUtils.indexOf("aabaabaa", 'a') = 0
 * StringUtils.indexOf("aabaabaa", 'b') = 2
 * </pre>
 *
 * @param seq  the CharSequence to check, may be null
 * @param searchChar  the character to find
 * @return the first index of the search character,
 *  -1 if no match or {@code null} string input
 * @since 2.0
 * @since 3.0 Changed signature from indexOf(String, int) to indexOf(CharSequence, int)
 */
public static int indexOf(CharSequence seq, int searchChar) {
    if (isEmpty(seq)) {
        return INDEX_NOT_FOUND;
    }
    return CharSequenceUtils.indexOf(seq, searchChar, 0);
}

From source file:org.apache.commons.lang3.StringUtils.java

/**
 * <p>Finds the first index within a CharSequence from a start position,
 * handling {@code null}./*from  w  w w.j  av  a  2s .c  o  m*/
 * This method uses {@link String#indexOf(int, int)} if possible.</p>
 *
 * <p>A {@code null} or empty ("") CharSequence will return {@code (INDEX_NOT_FOUND) -1}.
 * A negative start position is treated as zero.
 * A start position greater than the string length returns {@code -1}.</p>
 *
 * <pre>
 * StringUtils.indexOf(null, *, *)          = -1
 * StringUtils.indexOf("", *, *)            = -1
 * StringUtils.indexOf("aabaabaa", 'b', 0)  = 2
 * StringUtils.indexOf("aabaabaa", 'b', 3)  = 5
 * StringUtils.indexOf("aabaabaa", 'b', 9)  = -1
 * StringUtils.indexOf("aabaabaa", 'b', -1) = 2
 * </pre>
 *
 * @param seq  the CharSequence to check, may be null
 * @param searchChar  the character to find
 * @param startPos  the start position, negative treated as zero
 * @return the first index of the search character,
 *  -1 if no match or {@code null} string input
 * @since 2.0
 * @since 3.0 Changed signature from indexOf(String, int, int) to indexOf(CharSequence, int, int)
 */
public static int indexOf(CharSequence seq, int searchChar, int startPos) {
    if (isEmpty(seq)) {
        return INDEX_NOT_FOUND;
    }
    return CharSequenceUtils.indexOf(seq, searchChar, startPos);
}

From source file:org.apache.commons.lang3.StringUtils.java

/**
 * <p>Finds the first index within a CharSequence, handling {@code null}.
 * This method uses {@link String#indexOf(String, int)} if possible.</p>
 *
 * <p>A {@code null} CharSequence will return {@code -1}.</p>
 *
 * <pre>// www  .  jav a2  s.c o  m
 * StringUtils.indexOf(null, *)          = -1
 * StringUtils.indexOf(*, null)          = -1
 * StringUtils.indexOf("", "")           = 0
 * StringUtils.indexOf("", *)            = -1 (except when * = "")
 * StringUtils.indexOf("aabaabaa", "a")  = 0
 * StringUtils.indexOf("aabaabaa", "b")  = 2
 * StringUtils.indexOf("aabaabaa", "ab") = 1
 * StringUtils.indexOf("aabaabaa", "")   = 0
 * </pre>
 *
 * @param seq  the CharSequence to check, may be null
 * @param searchSeq  the CharSequence to find, may be null
 * @return the first index of the search CharSequence,
 *  -1 if no match or {@code null} string input
 * @since 2.0
 * @since 3.0 Changed signature from indexOf(String, String) to indexOf(CharSequence, CharSequence)
 */
public static int indexOf(CharSequence seq, CharSequence searchSeq) {
    if (seq == null || searchSeq == null) {
        return INDEX_NOT_FOUND;
    }
    return CharSequenceUtils.indexOf(seq, searchSeq, 0);
}

From source file:org.apache.commons.lang3.StringUtils.java

/**
 * <p>Finds the first index within a CharSequence, handling {@code null}.
 * This method uses {@link String#indexOf(String, int)} if possible.</p>
 *
 * <p>A {@code null} CharSequence will return {@code -1}.
 * A negative start position is treated as zero.
 * An empty ("") search CharSequence always matches.
 * A start position greater than the string length only matches
 * an empty search CharSequence.</p>
 *
 * <pre>/*ww  w . java  2  s. c om*/
 * StringUtils.indexOf(null, *, *)          = -1
 * StringUtils.indexOf(*, null, *)          = -1
 * StringUtils.indexOf("", "", 0)           = 0
 * StringUtils.indexOf("", *, 0)            = -1 (except when * = "")
 * StringUtils.indexOf("aabaabaa", "a", 0)  = 0
 * StringUtils.indexOf("aabaabaa", "b", 0)  = 2
 * StringUtils.indexOf("aabaabaa", "ab", 0) = 1
 * StringUtils.indexOf("aabaabaa", "b", 3)  = 5
 * StringUtils.indexOf("aabaabaa", "b", 9)  = -1
 * StringUtils.indexOf("aabaabaa", "b", -1) = 2
 * StringUtils.indexOf("aabaabaa", "", 2)   = 2
 * StringUtils.indexOf("abc", "", 9)        = 3
 * </pre>
 *
 * @param seq  the CharSequence to check, may be null
 * @param searchSeq  the CharSequence to find, may be null
 * @param startPos  the start position, negative treated as zero
 * @return the first index of the search CharSequence,
 *  -1 if no match or {@code null} string input
 * @since 2.0
 * @since 3.0 Changed signature from indexOf(String, String, int) to indexOf(CharSequence, CharSequence, int)
 */
public static int indexOf(CharSequence seq, CharSequence searchSeq, int startPos) {
    if (seq == null || searchSeq == null) {
        return INDEX_NOT_FOUND;
    }
    return CharSequenceUtils.indexOf(seq, searchSeq, startPos);
}

From source file:org.apache.commons.lang3.StringUtils.java

/**
 * <p>Finds the n-th index within a String, handling {@code null}.
 * This method uses {@link String#indexOf(String)} if possible.</p>
 *
 * <p>A {@code null} CharSequence will return {@code -1}.</p>
 *
 * @param str  the CharSequence to check, may be null
 * @param searchStr  the CharSequence to find, may be null
 * @param ordinal  the n-th {@code searchStr} to find
 * @param lastIndex true if lastOrdinalIndexOf() otherwise false if ordinalIndexOf()
 * @return the n-th index of the search CharSequence,
 *  {@code -1} ({@code INDEX_NOT_FOUND}) if no match or {@code null} string input
 *///  w  w  w  .j av  a 2  s.c o m
// Shared code between ordinalIndexOf(String,String,int) and lastOrdinalIndexOf(String,String,int)
private static int ordinalIndexOf(CharSequence str, CharSequence searchStr, int ordinal, boolean lastIndex) {
    if (str == null || searchStr == null || ordinal <= 0) {
        return INDEX_NOT_FOUND;
    }
    if (searchStr.length() == 0) {
        return lastIndex ? str.length() : 0;
    }
    int found = 0;
    int index = lastIndex ? str.length() : INDEX_NOT_FOUND;
    do {
        if (lastIndex) {
            index = CharSequenceUtils.lastIndexOf(str, searchStr, index - 1);
        } else {
            index = CharSequenceUtils.indexOf(str, searchStr, index + 1);
        }
        if (index < 0) {
            return index;
        }
        found++;
    } while (found < ordinal);
    return index;
}

From source file:org.apache.commons.lang3.StringUtils.java

/**
 * <p>Checks if CharSequence contains a search character, handling {@code null}.
 * This method uses {@link String#indexOf(int)} if possible.</p>
 *
 * <p>A {@code null} or empty ("") CharSequence will return {@code false}.</p>
 *
 * <pre>/*from w  w w. j  ava 2 s .  co m*/
 * StringUtils.contains(null, *)    = false
 * StringUtils.contains("", *)      = false
 * StringUtils.contains("abc", 'a') = true
 * StringUtils.contains("abc", 'z') = false
 * </pre>
 *
 * @param seq  the CharSequence to check, may be null
 * @param searchChar  the character to find
 * @return true if the CharSequence contains the search character,
 *  false if not or {@code null} string input
 * @since 2.0
 * @since 3.0 Changed signature from contains(String, int) to contains(CharSequence, int)
 */
public static boolean contains(CharSequence seq, int searchChar) {
    if (isEmpty(seq)) {
        return false;
    }
    return CharSequenceUtils.indexOf(seq, searchChar, 0) >= 0;
}

From source file:org.apache.commons.lang3.StringUtils.java

/**
 * <p>Checks if CharSequence contains a search CharSequence, handling {@code null}.
 * This method uses {@link String#indexOf(String)} if possible.</p>
 *
 * <p>A {@code null} CharSequence will return {@code false}.</p>
 *
 * <pre>//from   ww w . j ava  2  s . c o m
 * StringUtils.contains(null, *)     = false
 * StringUtils.contains(*, null)     = false
 * StringUtils.contains("", "")      = true
 * StringUtils.contains("abc", "")   = true
 * StringUtils.contains("abc", "a")  = true
 * StringUtils.contains("abc", "z")  = false
 * </pre>
 *
 * @param seq  the CharSequence to check, may be null
 * @param searchSeq  the CharSequence to find, may be null
 * @return true if the CharSequence contains the search CharSequence,
 *  false if not or {@code null} string input
 * @since 2.0
 * @since 3.0 Changed signature from contains(String, String) to contains(CharSequence, CharSequence)
 */
public static boolean contains(CharSequence seq, CharSequence searchSeq) {
    if (seq == null || searchSeq == null) {
        return false;
    }
    return CharSequenceUtils.indexOf(seq, searchSeq, 0) >= 0;
}

From source file:org.apache.commons.lang3.StringUtils.java

/**
 * <p>Search a CharSequence to find the first index of any
 * character not in the given set of characters.</p>
 *
 * <p>A {@code null} CharSequence will return {@code -1}.
 * A {@code null} or empty search string will return {@code -1}.</p>
 *
 * <pre>/*from ww  w .ja  v  a 2  s . com*/
 * StringUtils.indexOfAnyBut(null, *)            = -1
 * StringUtils.indexOfAnyBut("", *)              = -1
 * StringUtils.indexOfAnyBut(*, null)            = -1
 * StringUtils.indexOfAnyBut(*, "")              = -1
 * StringUtils.indexOfAnyBut("zzabyycdxx", "za") = 3
 * StringUtils.indexOfAnyBut("zzabyycdxx", "")   = -1
 * StringUtils.indexOfAnyBut("aba","ab")         = -1
 * </pre>
 *
 * @param seq  the CharSequence to check, may be null
 * @param searchChars  the chars to search for, may be null
 * @return the index of any of the chars, -1 if no match or null input
 * @since 2.0
 * @since 3.0 Changed signature from indexOfAnyBut(String, String) to indexOfAnyBut(CharSequence, CharSequence)
 */
public static int indexOfAnyBut(CharSequence seq, CharSequence searchChars) {
    if (isEmpty(seq) || isEmpty(searchChars)) {
        return INDEX_NOT_FOUND;
    }
    int strLen = seq.length();
    for (int i = 0; i < strLen; i++) {
        char ch = seq.charAt(i);
        boolean chFound = CharSequenceUtils.indexOf(searchChars, ch, 0) >= 0;
        if (i + 1 < strLen && Character.isHighSurrogate(ch)) {
            char ch2 = seq.charAt(i + 1);
            if (chFound && CharSequenceUtils.indexOf(searchChars, ch2, 0) < 0) {
                return i;
            }
        } else {
            if (!chFound) {
                return i;
            }
        }
    }
    return INDEX_NOT_FOUND;
}

From source file:org.apache.commons.lang3.StringUtils.java

/**
 * <p>Find the first index of any of a set of potential substrings.</p>
 *
 * <p>A {@code null} CharSequence will return {@code -1}.
 * A {@code null} or zero length search array will return {@code -1}.
 * A {@code null} search array entry will be ignored, but a search
 * array containing "" will return {@code 0} if {@code str} is not
 * null. This method uses {@link String#indexOf(String)} if possible.</p>
 *
 * <pre>/*from  w w  w  . j  a v a  2  s  .  c o m*/
 * StringUtils.indexOfAny(null, *)                     = -1
 * StringUtils.indexOfAny(*, null)                     = -1
 * StringUtils.indexOfAny(*, [])                       = -1
 * StringUtils.indexOfAny("zzabyycdxx", ["ab","cd"])   = 2
 * StringUtils.indexOfAny("zzabyycdxx", ["cd","ab"])   = 2
 * StringUtils.indexOfAny("zzabyycdxx", ["mn","op"])   = -1
 * StringUtils.indexOfAny("zzabyycdxx", ["zab","aby"]) = 1
 * StringUtils.indexOfAny("zzabyycdxx", [""])          = 0
 * StringUtils.indexOfAny("", [""])                    = 0
 * StringUtils.indexOfAny("", ["a"])                   = -1
 * </pre>
 *
 * @param str  the CharSequence to check, may be null
 * @param searchStrs  the CharSequences to search for, may be null
 * @return the first index of any of the searchStrs in str, -1 if no match
 * @since 3.0 Changed signature from indexOfAny(String, String[]) to indexOfAny(CharSequence, CharSequence...)
 */
public static int indexOfAny(CharSequence str, CharSequence... searchStrs) {
    if (str == null || searchStrs == null) {
        return INDEX_NOT_FOUND;
    }
    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++) {
        CharSequence search = searchStrs[i];
        if (search == null) {
            continue;
        }
        tmp = CharSequenceUtils.indexOf(str, search, 0);
        if (tmp == INDEX_NOT_FOUND) {
            continue;
        }

        if (tmp < ret) {
            ret = tmp;
        }
    }

    return ret == Integer.MAX_VALUE ? INDEX_NOT_FOUND : ret;
}

From source file:org.apache.commons.lang3.StringUtils.java

/**
 * <p>Counts how many times the substring appears in the larger string.</p>
 *
 * <p>A {@code null} or empty ("") String input returns {@code 0}.</p>
 *
 * <pre>//  w w  w  .j av a 2 s . c o m
 * StringUtils.countMatches(null, *)       = 0
 * StringUtils.countMatches("", *)         = 0
 * StringUtils.countMatches("abba", null)  = 0
 * StringUtils.countMatches("abba", "")    = 0
 * StringUtils.countMatches("abba", "a")   = 2
 * StringUtils.countMatches("abba", "ab")  = 1
 * StringUtils.countMatches("abba", "xxx") = 0
 * </pre>
 *
 * @param str  the CharSequence to check, may be null
 * @param sub  the substring to count, may be null
 * @return the number of occurrences, 0 if either CharSequence is {@code null}
 * @since 3.0 Changed signature from countMatches(String, String) to countMatches(CharSequence, CharSequence)
 */
public static int countMatches(CharSequence str, CharSequence sub) {
    if (isEmpty(str) || isEmpty(sub)) {
        return 0;
    }
    int count = 0;
    int idx = 0;
    while ((idx = CharSequenceUtils.indexOf(str, sub, idx)) != INDEX_NOT_FOUND) {
        count++;
        idx += sub.length();
    }
    return count;
}