Issue
I'm trying to use substringBefore() fun with two char ("#" and "&"). How can i use regex for both cahrs? I dont want to do this:
if (myString.contains("&")) {
myString.substringBefore("&")
} else if (myString.contains("#"))
myString.substringBefore("#")
}
Solution
Finally I've found a way and it is actually easy:
val matchRegex = "[#&]".toRegex().find(myString)
val manipulatedString = matchRegex?.range?.first?.let {
myString.substring(0, it)
}
This will return NULL if there is no regex chars in the string or, the subString before those chars
Answered By - Evyatar Cohen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.