I have a string that is built out of three parts. The word I want the string to be (changes), a seperating part (doesn't change) and the last part which changes. I want to delete the seperating part and the ending part. The seperating part is " - " so what I'm wondering is if theres a way to delete everything after a certaint part of the string.
An example of this scenario would be if I wanted to turn this: "Stack Overflow - A place to ask stuff" into this: "Stack Overflow". Any help is appreciated!
For example, you could do:
or
(and add relevant error handling)
you can my utils method this action..
The apache commons StringUtils provide a substringBefore method
StringUtils.substringBefore("Stack Overflow - A place to ask stuff", " - ")
Perhaps thats what you are looking for:
Clean way to safely remove until a string, and keep the searched part if token may or may not exist.
Kotlin Solution
Use the built-in Kotlin
substringBefore
function (Documentation):It also has an optional second param, which is the return value if the delimiter is not found. The default value is the original string