You need to use StringUtils.replace, StringUtils.replaceOnce, etc.

The semantics of the K functions are as follows:

replace(input, pattern, replacement, n) replaces n times
replaceFirst replaces once
replaceAll replaces every instance

your countOcurrences looks correct though.