Kotlin replace function in String class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Comments
Post a Comment