Applying my patch from LANG-469 - adding a lastOrdinalIndexOf method to complement the existing ordinalIndexOf method
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@891142 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
aa5d2162e5
commit
425dcadca5
|
@ -831,6 +831,12 @@ public class StringUtils {
|
|||
* StringUtils.ordinalIndexOf("aabaabaa", "", 2) = 0
|
||||
* </pre>
|
||||
*
|
||||
* <p>Note that 'head(String str, int n)' may be implemented as: </p>
|
||||
*
|
||||
* <pre>
|
||||
* str.substring(0, lastOrdinalIndexOf(str, "\n", n))
|
||||
* </pre>
|
||||
*
|
||||
* @param str the String to check, may be null
|
||||
* @param searchStr the String to find, may be null
|
||||
* @param ordinal the n-th <code>searchStr</code> to find
|
||||
|
@ -839,16 +845,25 @@ public class StringUtils {
|
|||
* @since 2.1
|
||||
*/
|
||||
public static int ordinalIndexOf(String str, String searchStr, int ordinal) {
|
||||
return ordinalIndexOf(str, searchStr, ordinal, false);
|
||||
}
|
||||
|
||||
// Shared code between ordinalIndexOf(String,String,int) and lastOrdinalIndexOf(String,String,int)
|
||||
private static int ordinalIndexOf(String str, String searchStr, int ordinal, boolean lastIndex) {
|
||||
if (str == null || searchStr == null || ordinal <= 0) {
|
||||
return INDEX_NOT_FOUND;
|
||||
}
|
||||
if (searchStr.length() == 0) {
|
||||
return 0;
|
||||
return lastIndex ? str.length() : 0;
|
||||
}
|
||||
int found = 0;
|
||||
int index = INDEX_NOT_FOUND;
|
||||
int index = lastIndex ? str.length() : INDEX_NOT_FOUND;
|
||||
do {
|
||||
index = str.indexOf(searchStr, index + 1);
|
||||
if(lastIndex) {
|
||||
index = str.lastIndexOf(searchStr, index - 1);
|
||||
} else {
|
||||
index = str.indexOf(searchStr, index + 1);
|
||||
}
|
||||
if (index < 0) {
|
||||
return index;
|
||||
}
|
||||
|
@ -990,6 +1005,43 @@ public class StringUtils {
|
|||
return str.lastIndexOf(searchStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Finds the n-th last index within a String, handling <code>null</code>.
|
||||
* This method uses {@link String#lastIndexOf(String)}.</p>
|
||||
*
|
||||
* <p>A <code>null</code> String will return <code>-1</code>.</p>
|
||||
*
|
||||
* <pre>
|
||||
* StringUtils.lastOrdinalIndexOf(null, *, *) = -1
|
||||
* StringUtils.lastOrdinalIndexOf(*, null, *) = -1
|
||||
* StringUtils.lastOrdinalIndexOf("", "", *) = 0
|
||||
* StringUtils.lastOrdinalIndexOf("aabaabaa", "a", 1) = 7
|
||||
* StringUtils.lastOrdinalIndexOf("aabaabaa", "a", 2) = 6
|
||||
* StringUtils.lastOrdinalIndexOf("aabaabaa", "b", 1) = 5
|
||||
* StringUtils.lastOrdinalIndexOf("aabaabaa", "b", 2) = 2
|
||||
* StringUtils.lastOrdinalIndexOf("aabaabaa", "ab", 1) = 4
|
||||
* StringUtils.lastOrdinalIndexOf("aabaabaa", "ab", 2) = 1
|
||||
* StringUtils.lastOrdinalIndexOf("aabaabaa", "", 1) = 8
|
||||
* StringUtils.lastOrdinalIndexOf("aabaabaa", "", 2) = 8
|
||||
* </pre>
|
||||
*
|
||||
* <p>Note that 'tail(String str, int n)' may be implemented as: </p>
|
||||
*
|
||||
* <pre>
|
||||
* str.substring(lastOrdinalIndexOf(str, "\n", n) + 1)
|
||||
* </pre>
|
||||
*
|
||||
* @param str the String to check, may be null
|
||||
* @param searchStr the String to find, may be null
|
||||
* @param ordinal the n-th last <code>searchStr</code> to find
|
||||
* @return the n-th last index of the search String,
|
||||
* <code>-1</code> (<code>INDEX_NOT_FOUND</code>) if no match or <code>null</code> string input
|
||||
* @since 3.0
|
||||
*/
|
||||
public static int lastOrdinalIndexOf(String str, String searchStr, int ordinal) {
|
||||
return ordinalIndexOf(str, searchStr, ordinal, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Finds the first index within a String, handling <code>null</code>.
|
||||
* This method uses {@link String#lastIndexOf(String, int)}.</p>
|
||||
|
@ -5836,5 +5888,4 @@ public class StringUtils {
|
|||
int strOffset = str.length() - suffix.length();
|
||||
return str.regionMatches(ignoreCase, strOffset, suffix, 0, suffix.length());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -230,6 +230,20 @@ public class StringUtilsEqualsIndexOfTest extends TestCase {
|
|||
assertEquals(4, StringUtils.lastIndexOf("aabaabaa", "ab"));
|
||||
}
|
||||
|
||||
public void testLastOrdinalIndexOf() {
|
||||
assertEquals(-1, StringUtils.lastOrdinalIndexOf(null, "*", 42) );
|
||||
assertEquals(-1, StringUtils.lastOrdinalIndexOf("*", null, 42) );
|
||||
assertEquals(0, StringUtils.lastOrdinalIndexOf("", "", 42) );
|
||||
assertEquals(7, StringUtils.lastOrdinalIndexOf("aabaabaa", "a", 1) );
|
||||
assertEquals(6, StringUtils.lastOrdinalIndexOf("aabaabaa", "a", 2) );
|
||||
assertEquals(5, StringUtils.lastOrdinalIndexOf("aabaabaa", "b", 1) );
|
||||
assertEquals(2, StringUtils.lastOrdinalIndexOf("aabaabaa", "b", 2) );
|
||||
assertEquals(4, StringUtils.lastOrdinalIndexOf("aabaabaa", "ab", 1) );
|
||||
assertEquals(1, StringUtils.lastOrdinalIndexOf("aabaabaa", "ab", 2) );
|
||||
assertEquals(8, StringUtils.lastOrdinalIndexOf("aabaabaa", "", 1) );
|
||||
assertEquals(8, StringUtils.lastOrdinalIndexOf("aabaabaa", "", 2) );
|
||||
}
|
||||
|
||||
public void testLastIndexOf_StringInt() {
|
||||
assertEquals(-1, StringUtils.lastIndexOf(null, null, 0));
|
||||
assertEquals(-1, StringUtils.lastIndexOf(null, null, -1));
|
||||
|
|
Loading…
Reference in New Issue