Ayfri
01/05/2022, 1:11 PMfun String.remove(pattern: String) = replace(Regex(pattern), "")
fun String.remove(regex: Regex) = replace(regex, "")
Or at least the latter one ?
I also have in multiple projects this extension
fun String.get(regex: Regex) = replace(regex, "$1")
That could be useful to addJavier
01/05/2022, 1:47 PMsecond()
, secondOrNull()
and so onephemient
01/05/2022, 3:46 PM.remove
or .get
.mcpiroman
01/05/2022, 3:55 PMmatch(regex).groups[0]
? If so, imo you should either you that or name this extension more like getFirstGroup
.Klitos Kyriacou
01/05/2022, 4:29 PMfun String.remove(pattern: String) = replace(Regex(pattern), "")
), because that would be inconsistent with how strings are treated in Kotlin.
Unlike Java, Kotlin strings don't use implicit regexes. For example, whereas in Java, String.split() takes a regex as a string, in Kotlin the same function takes a literal string and does not interpret it as a regex. If you want it to be interpreted as a regex, pass a Regex argument.Javier
01/05/2022, 5:17 PMfun String.remove(value: String) = replace(value, "")