본문 바로가기
코틀린/[Elements] 요소 작업

[Kotlin][Collection] lastIndexOf

by jinwo_o 2024. 8. 21.

지정된 startIndex 부터 시작하여 지정된 문자가 마지막으로 나타나는 이 문자 시퀀스 내의 인덱스를 반환합니다.

반환 char 의 마지막 발생 인덱스를 반환하거나, 아무것도 발견되지 않으면 -1 을 반환합니다.

public fun CharSequence.lastIndexOf(string: String, startIndex: Int = lastIndex, ignoreCase: Boolean = false): Int {
    return if (ignoreCase || this !is String)
        indexOf(string, startIndex, 0, ignoreCase, last = true)
    else
        nativeLastIndexOf(string, startIndex)
}


val string = "HelloWorld"
print(string.lastIndexOf("abc")) // -1
print(string.lastIndexOf("Hello")) // 0
print(string.lastIndexOf("World")) // 5

 

lastIndexOf - Kotlin Programming Language

 

kotlinlang.org