indexOf

fun CharSequence.indexOf(char: Char, startIndex: Int = 0, ignoreCase: Boolean = false): Int(source)

Returns the index within this string of the first occurrence of the specified character, starting from the specified startIndex.

Since Kotlin

1.1

Return

An index of the first occurrence of char or -1 if none is found.

Parameters

ignoreCase

true to ignore character case when matching a character. By default false.


fun CharSequence.indexOf(string: String, startIndex: Int = 0, ignoreCase: Boolean = false): Int(source)

Returns the index within this char sequence of the first occurrence of the specified string, starting from the specified startIndex.

Since Kotlin

1.1

Return

An index of the first occurrence of string or -1 if none is found.

Parameters

ignoreCase

true to ignore character case when matching a string. By default false.

Samples

import java.util.Locale
import kotlin.test.*

fun main() { 
   //sampleStart 
   fun matchDetails(inputString: String, whatToFind: String, startIndex: Int = 0): String {
    val matchIndex = inputString.indexOf(whatToFind, startIndex)
    return "Searching for '$whatToFind' in '$inputString' starting at position $startIndex: " +
            if (matchIndex >= 0) "Found at $matchIndex" else "Not found"
}

val inputString = "Never ever give up"
val toFind = "ever"

println(matchDetails(inputString, toFind)) // Searching for 'ever' in 'Never ever give up' starting at position 0: Found at 1
println(matchDetails(inputString, toFind, 2)) // Searching for 'ever' in 'Never ever give up' starting at position 2: Found at 6
println(matchDetails(inputString, toFind, 10)) // Searching for 'ever' in 'Never ever give up' starting at position 10: Not found 
   //sampleEnd
}
fun CharSequence.indexOf(char: Char, startIndex: Int = 0, ignoreCase: Boolean = false): Int(source)

Returns the index within this string of the first occurrence of the specified character, starting from the specified startIndex.

Since Kotlin

1.0

Return

An index of the first occurrence of char or -1 if none is found.

Parameters

ignoreCase

true to ignore character case when matching a character. By default false.


fun CharSequence.indexOf(string: String, startIndex: Int = 0, ignoreCase: Boolean = false): Int(source)

Returns the index within this char sequence of the first occurrence of the specified string, starting from the specified startIndex.

Since Kotlin

1.0

Return

An index of the first occurrence of string or -1 if none is found.

Parameters

ignoreCase

true to ignore character case when matching a string. By default false.

Samples

import java.util.Locale
import kotlin.test.*

fun main() { 
   //sampleStart 
   fun matchDetails(inputString: String, whatToFind: String, startIndex: Int = 0): String {
    val matchIndex = inputString.indexOf(whatToFind, startIndex)
    return "Searching for '$whatToFind' in '$inputString' starting at position $startIndex: " +
            if (matchIndex >= 0) "Found at $matchIndex" else "Not found"
}

val inputString = "Never ever give up"
val toFind = "ever"

println(matchDetails(inputString, toFind)) // Searching for 'ever' in 'Never ever give up' starting at position 0: Found at 1
println(matchDetails(inputString, toFind, 2)) // Searching for 'ever' in 'Never ever give up' starting at position 2: Found at 6
println(matchDetails(inputString, toFind, 10)) // Searching for 'ever' in 'Never ever give up' starting at position 10: Not found 
   //sampleEnd
}