What is the most efficient way to split a string into two parts, in the following way
One part is the last word of string that follows last whitespace character in the string
Second part is rest of the string
e.g.
“This is a sentence”
one part: “sentence”
second part: “This is a ” //Note there is whitespace at the end of this string
“This is a ”
one part: “”
second part: “This is a “
Do you want to remove the last word, semantically speaking, or do you want to chop everything off after the last whitespace character, which is what you described? I’m asking because they’re not actually the same thing, depending on the language of the text.
If you want to chop off everything after the last bit of whitespace, the other answers here will do you fine. But if you want to chop off the last word, then you need to dig a little deeper and use the word enumeration APIs: