To remove a specific substring from a string in Kotlin, you can use the `replace` function of the String class.
In this example,
str
with a value of "Hello World"
.replace
function to replace the substring "World"
with an empty string ""
, effectively removing it.replace
function returns a new string with the specified replacements.str
to standard output.fun main() {
var str = "Hello World"
str = str.replace("World", "")
println("Modified string: $str")
}
Modified string: Hello
In this example,
text
with a value of "I like apple pie"
.replace
function to replace the substring "apple"
with an empty string ""
.replace
function creates and returns a new string with the specified replacements.text
to standard output.fun main() {
var text = "I like apple pie"
text = text.replace("apple", "")
println("Modified string: $text")
}
Modified string: I like pie
In this tutorial, we learned How to Remove specific Substring in Kotlin language with well detailed examples.