Different ways to get substring in a string in Kotlin

Different ways to get substring in a string in Kotlin :

Kotlin string comes with different utility methods to extract one substring. These utility methods or extensions functions are better than what Java provides and they can get you substrings based on different conditions. In this post, I will show you how to use these Kotlin substring extension functions with examples.

fun String.substring(startIndex: Int) : String

This method will return one substring starting from index startIndex to the end of the string. If the startIndex is invalid, it will throw one java.lang.StringIndexOutOfBoundsException.

Example :

fun main() {
    val str = "Hello World !!"
    println(str.substring(4))
    println(str.substring(7))
}
It will print :
o World !!
orld !!

String.substring(startIndex: Int, endIndex: Int): String

This is similar to the above method. The only difference is that it takes one more parameter endIndex to mark the ending index of the substring. It returns one string starting from startIndex to endIndex excluding the character at endIndex.

fun main() {
    val str = "Hello World !!"
    println(str.substring(4,7))
    println(str.substring(7,10))
}
It will print :
o W
orl

It throws java.lang.StringIndexOutOfBoundsException if any of the index is invalid.

fun String.substring(range: IntRange): String

It returns one substring specified by the range of integers defined by range.

For example :

fun main() {
    val str = "Hello World !!"
    println(str.substring(1..5))
}
Output :
ello

fun String.substringAfter(delimiter, missingDelimiterValue) : String

This method is to get one substring from a string after a character or string. It will return one substring after the first occurrence of the delimiter value. This value can be a character or a string. If the provided delimiter is not found, it will return missingDelimiterValue. This is optional and if we don’t provide it, the full string is returned.

fun main() {
    val str = "Hello %World !!"

    println(str.substringAfter('%'))
    println(str.substringAfter("Hello"))
    println(str.substringAfter('*'))
    println(str.substringAfter('*',"Not Found"))
}
Output :
World !!
 %World !!
Hello %World !!
Not Found
  • The first print statement prints the substring after the character %
  • The second print statement prints the substring after the string Hello
  • The third print statement prints the string str because character * is not in it.
  • The last print statement prints “Not Found” because * is not found in the string.

fun String.substringBefore(delimiter, missingDelimiterValue) : String

This is similar to the above method. Using it, you can get one sub-string before a defined delimiter parameter. If the string doesn’t contain the delimiter, it returns missingDelimiter, which is an optional value and by default it is the original string.

Let’s try the same example we have used before :

fun main() {
    val str = "Hello %World !!"

    println(str.substringBefore('%'))
    println(str.substringBefore("Hello"))
    println(str.substringBefore('*'))
    println(str.substringBefore('*',"Not Found"))
}
It will print :
Hello

Hello %World !!
Not Found

substringBeforeLast and substringAfterLast :

These methods are similar to substringBefore and substringAfter. The only difference is that these are used to return the substrings before or after the last occurrence of the delimiter. The parameters are same :

fun main() {
    val str = "https://codevscolor.com/example"

    println(str.substringBeforeLast('/'))
    println(str.substringAfterLast('/'))
}
Output :
https://codevscolor.com
example

Similar tutorials :