To get the substring before a specific character in Kotlin, you can use the `substringBefore` method of the String class.
In this example,
url
with a value of "https://example.com/page"
.substringBefore
method with the character '/'
to get the substring before the specified character.fun main() {
val url = "https://example.com/page"
val substring = url.substringBefore('/')
println("Substring before '/': $substring")
}
Substring before '/': https:
In this example,
str
with a value of "Hello,World"
.substringBefore
method with the character ','
to get the substring before the specified character.fun main() {
val str = "Hello,World"
val substring = str.substringBefore(',')
println("Substring before ',': $substring")
}
Substring before ',': Hello
In this tutorial, we learned How to get Substring before Specific Character in Kotlin language with well detailed examples.