mapIndexedNotNull

Returns a list containing only the non-null results of applying the given transform function to each character and its index in the original char sequence.

inline fun <R : Any> CharSequence.mapIndexedNotNull(transform: (index: Int, Char) -> R?): List<R>(source)
val text = "hello world 123"

val digitIndices = text.mapIndexedNotNull { index, char ->
    if (char.isDigit()) index else null
}

println(digitIndices)   // [15, 16, 17]

Source