i have the following code
Pattern keyPattern = Pattern.compile(key);
Matcher matcher = keyPattern.matcher(str);
return matcher.replaceAll(value);
this will replaces the key in the str with value;
but i want to know how many instances of key’s has been replaced with value.
so how to know that?
You can use the find method in a loop to count. There is an example in the Java tutorial:
While you are calling find, you can at the same time collect the parts of the string and manually build the result in a StringBuffer. Or if performance is not an issue, you can first count and then afterwards scan the string again with replaceAll.