Kotlin replace function in String class

fun main(args: Array<String>) {
/* Replace
* 1, a substring
* 2, Regular Expression (Regex)
* 3, Replace Character
* 4, Replace First*/
/**** Substring Replace ***/
val st = "Hello World"
val nst = st.replace("World", "Kotlin")
println(st)
println(nst)
/**** Regular Expression ***/
val str = "abc2342dc234"
var newStr = str.replace(Regex("\\d+"),
"X")
/**** Replace character ***/
var newStrr = str.replace("a",
"X")
println(str)
println(newStr)
println(newStrr)
/**** ReplaceFirst ***/
val a = "Hello guys Hello World"
val b = st.replace("Hello", "Hi", true)
println(a)
println(b)
}
view raw gistfile1.txt hosted with ❤ by GitHub

Comments

Popular posts from this blog

Your build is currently configured to use incompatible Java 21.0.3 and Gradle 8.2.1. Cannot sync the project.

Google Assistant Implementation in Android application with app actions