List of usage examples for org.apache.commons.lang3 CharSequenceUtils lastIndexOf
static int lastIndexOf(final CharSequence cs, final CharSequence searchChar, final int start)
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 ww .j a v a 2 s . co 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>Finds the last index within a CharSequence, handling {@code null}. * This method uses {@link String#lastIndexOf(int)} if possible.</p> * * <p>A {@code null} or empty ("") CharSequence will return {@code -1}.</p> * * <pre>//from w w w .ja v a 2s. c om * StringUtils.lastIndexOf(null, *) = -1 * StringUtils.lastIndexOf("", *) = -1 * StringUtils.lastIndexOf("aabaabaa", 'a') = 7 * StringUtils.lastIndexOf("aabaabaa", 'b') = 5 * </pre> * * @param seq the CharSequence to check, may be null * @param searchChar the character to find * @return the last index of the search character, * -1 if no match or {@code null} string input * @since 2.0 * @since 3.0 Changed signature from lastIndexOf(String, int) to lastIndexOf(CharSequence, int) */ public static int lastIndexOf(CharSequence seq, int searchChar) { if (isEmpty(seq)) { return INDEX_NOT_FOUND; } return CharSequenceUtils.lastIndexOf(seq, searchChar, seq.length()); }
From source file:org.apache.commons.lang3.StringUtils.java
/** * <p>Finds the last index within a CharSequence from a start position, * handling {@code null}.//www . j a v a 2s. co m * This method uses {@link String#lastIndexOf(int, int)} if possible.</p> * * <p>A {@code null} or empty ("") CharSequence will return {@code -1}. * A negative start position returns {@code -1}. * A start position greater than the string length searches the whole string.</p> * * <pre> * StringUtils.lastIndexOf(null, *, *) = -1 * StringUtils.lastIndexOf("", *, *) = -1 * StringUtils.lastIndexOf("aabaabaa", 'b', 8) = 5 * StringUtils.lastIndexOf("aabaabaa", 'b', 4) = 2 * StringUtils.lastIndexOf("aabaabaa", 'b', 0) = -1 * StringUtils.lastIndexOf("aabaabaa", 'b', 9) = 5 * StringUtils.lastIndexOf("aabaabaa", 'b', -1) = -1 * StringUtils.lastIndexOf("aabaabaa", 'a', 0) = 0 * </pre> * * @param seq the CharSequence to check, may be null * @param searchChar the character to find * @param startPos the start position * @return the last index of the search character, * -1 if no match or {@code null} string input * @since 2.0 * @since 3.0 Changed signature from lastIndexOf(String, int, int) to lastIndexOf(CharSequence, int, int) */ public static int lastIndexOf(CharSequence seq, int searchChar, int startPos) { if (isEmpty(seq)) { return INDEX_NOT_FOUND; } return CharSequenceUtils.lastIndexOf(seq, searchChar, startPos); }
From source file:org.apache.commons.lang3.StringUtils.java
/** * <p>Finds the last index within a CharSequence, handling {@code null}. * This method uses {@link String#lastIndexOf(String)} if possible.</p> * * <p>A {@code null} CharSequence will return {@code -1}.</p> * * <pre>//from ww w .j a v a2 s.c om * StringUtils.lastIndexOf(null, *) = -1 * StringUtils.lastIndexOf(*, null) = -1 * StringUtils.lastIndexOf("", "") = 0 * StringUtils.lastIndexOf("aabaabaa", "a") = 7 * StringUtils.lastIndexOf("aabaabaa", "b") = 5 * StringUtils.lastIndexOf("aabaabaa", "ab") = 4 * StringUtils.lastIndexOf("aabaabaa", "") = 8 * </pre> * * @param seq the CharSequence to check, may be null * @param searchSeq the CharSequence to find, may be null * @return the last index of the search String, * -1 if no match or {@code null} string input * @since 2.0 * @since 3.0 Changed signature from lastIndexOf(String, String) to lastIndexOf(CharSequence, CharSequence) */ public static int lastIndexOf(CharSequence seq, CharSequence searchSeq) { if (seq == null || searchSeq == null) { return INDEX_NOT_FOUND; } return CharSequenceUtils.lastIndexOf(seq, searchSeq, seq.length()); }
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#lastIndexOf(String, int)} if possible.</p> * * <p>A {@code null} CharSequence will return {@code -1}. * A negative start position returns {@code -1}. * An empty ("") search CharSequence always matches unless the start position is negative. * A start position greater than the string length searches the whole string.</p> * * <pre>//from w ww .j a v a 2s .c om * StringUtils.lastIndexOf(null, *, *) = -1 * StringUtils.lastIndexOf(*, null, *) = -1 * StringUtils.lastIndexOf("aabaabaa", "a", 8) = 7 * StringUtils.lastIndexOf("aabaabaa", "b", 8) = 5 * StringUtils.lastIndexOf("aabaabaa", "ab", 8) = 4 * StringUtils.lastIndexOf("aabaabaa", "b", 9) = 5 * StringUtils.lastIndexOf("aabaabaa", "b", -1) = -1 * StringUtils.lastIndexOf("aabaabaa", "a", 0) = 0 * StringUtils.lastIndexOf("aabaabaa", "b", 0) = -1 * </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 lastIndexOf(String, String, int) to lastIndexOf(CharSequence, CharSequence, int) */ public static int lastIndexOf(CharSequence seq, CharSequence searchSeq, int startPos) { if (seq == null || searchSeq == null) { return INDEX_NOT_FOUND; } return CharSequenceUtils.lastIndexOf(seq, searchSeq, startPos); }
From source file:org.apache.commons.lang3.StringUtils.java
/** * <p>Find the latest index of any of a set of potential substrings.</p> * * <p>A {@code null} CharSequence will return {@code -1}. * A {@code null} search array will return {@code -1}. * A {@code null} or zero length search array entry will be ignored, * but a search array containing "" will return the length of {@code str} * if {@code str} is not null. This method uses {@link String#indexOf(String)} if possible</p> * * <pre>//w w w.java2 s .com * StringUtils.lastIndexOfAny(null, *) = -1 * StringUtils.lastIndexOfAny(*, null) = -1 * StringUtils.lastIndexOfAny(*, []) = -1 * StringUtils.lastIndexOfAny(*, [null]) = -1 * StringUtils.lastIndexOfAny("zzabyycdxx", ["ab","cd"]) = 6 * StringUtils.lastIndexOfAny("zzabyycdxx", ["cd","ab"]) = 6 * StringUtils.lastIndexOfAny("zzabyycdxx", ["mn","op"]) = -1 * StringUtils.lastIndexOfAny("zzabyycdxx", ["mn","op"]) = -1 * StringUtils.lastIndexOfAny("zzabyycdxx", ["mn",""]) = 10 * </pre> * * @param str the CharSequence to check, may be null * @param searchStrs the CharSequences to search for, may be null * @return the last index of any of the CharSequences, -1 if no match * @since 3.0 Changed signature from lastIndexOfAny(String, String[]) to lastIndexOfAny(CharSequence, CharSequence) */ public static int lastIndexOfAny(CharSequence str, CharSequence... searchStrs) { if (str == null || searchStrs == null) { return INDEX_NOT_FOUND; } int sz = searchStrs.length; int ret = INDEX_NOT_FOUND; int tmp = 0; for (int i = 0; i < sz; i++) { CharSequence search = searchStrs[i]; if (search == null) { continue; } tmp = CharSequenceUtils.lastIndexOf(str, search, str.length()); if (tmp > ret) { ret = tmp; } } return ret; }