Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/main/java/org/apache/commons/lang3/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9245,5 +9245,25 @@ public static int[] toCodePoints(CharSequence str) {
index += Character.charCount(result[i]);
}
return result;
}

/**
* <p>Finds index of all the occurences of given search key found in source string.
* </p>
* @param source
* @param searchKey
* @return list of integer of indexes.
*/
public static List<Integer> indexesOf(final CharSequence source, final Character searchKey) {
if(isEmpty(source) || searchKey == CharUtils.NUL ) {
return null;
}
List<Integer> indexList = new ArrayList<>();
for(int i = 0 ; i < source.length() ; i++) {
if(searchKey.equals(source.charAt(i))) {
indexList.add(i);
}
}
return indexList;
}
}
9 changes: 9 additions & 0 deletions src/test/java/org/apache/commons/lang3/StringUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
Expand Down Expand Up @@ -3229,6 +3230,14 @@ public void testToCodePoints() throws Exception {
assertNull(StringUtils.toCodePoints(null));
assertArrayEquals(ArrayUtils.EMPTY_INT_ARRAY, StringUtils.toCodePoints(""));
}

@Test
public void testIndexesOf() throws Exception {
assertEquals(null, 3, StringUtils.indexesOf("anagram", 'a').size());
assertEquals(null, 2, StringUtils.indexesOf("Reader", 'e').size());
assertNull(StringUtils.indexesOf("people", null));
assertNotEquals(null, 2, StringUtils.indexesOf("Automatic", 'o').size());
}

@Test
public void testGetDigits() {
Expand Down