To find the index of a substring in Kotlin, you can use the indexOf
method of the String class.
In this example,
str
with a string value.indexOf
method of the string object str
to find the index of a specific substring.indexOf
method returns the starting index of the substring in the string.fun main(args: Array<String>) {
val str = "Hello World";
val index = str.indexOf("World")
print("Index of substring is: $index");
}
Index of substring is: 6
In this example,
str1
and str2
with different values.indexOf
method of str1
to find the index of str2
within str1
.indexOf
method returns the starting index of str2
in str1
.fun main(args: Array<String>) {
val str1 = "Hello World";
val str2 = "World";
val index = str1.indexOf(str2)
print("Index of substring is: $index");
}
Index of substring is: 6
In this tutorial, we learned How to find the Index of Substring in Kotlin language with well detailed examples.