Does anyone knows how to replace the last word in a String.
Currently I am doing:
someStr = someStr.replace(someStr.substring(someStr.lastIndexOf(" ") + 1), "New Word");
The above code replaces every single occurance of the word in the string.
Thanks.
You could create a new string “from scratch” like this:
Another option (if you really want to use “replace” 🙂 is to do
replaceAlluses regular expressions and\S*$means a space, followed by some non-space characters, followed by end of string. (That is, replace the characters after the last space.)