To remove a specific character from a string in Kotlin, you can use the `replace` function.
In this example,
str1
with a value of "Hello World"
.replace
function to replace all occurrences of 'l' with an empty string in str1
.replace
function returns a new string with the specified characters replaced.str1
to standard output.fun main() {
val str1 = "Hello World"
val modifiedStr = str1.replace("l", "")
println("Modified string: $modifiedStr")
}
Modified string: Heo Word
In this example,
str2
with a value of "apple"
.replace
function to replace all occurrences of 'a' with an empty string in str2
.replace
function returns a new string with the specified characters replaced.str2
to standard output.fun main() {
val str2 = "apple"
val modifiedStr = str2.replace("a", "")
println("Modified string: $modifiedStr")
}
Modified string: pple
In this tutorial, we learned How to Remove specific Character from a String in Kotlin language with well detailed examples.