How to set the caret at the end of the replacement

The first line in your code sample is incomplete. But the third and sixth call to setCursor both conform to the signature in the docs and will work.