I've got a very long java string and I'm trying to replace half of the occurrences of a particular word with its reverse.
So, if the string is "hello my friend hello", the output if I run it with the word "hello" should give something like "hello my friend olleh".
I know that I can reverse a word with
new StringBuilder(str).reverse().toString()
and that I can use
Str.replaceAll("hello", "olleh")
to modify the string. But how can I tell replaceAll to only substitute half of the occurrences of hello? Is there a way to input a probability in replaceAll or loop through the text and stop at every occurrence?