Why do you have an if statement? replaceAll() won't replace anything that isn't there. Of course, you may be worried about the cost of calling replaceAll() when there is nothing to replace. Does this happen very often? The current paradigm is to not worry about such optimizations until after you run code metrics to determine if it will make a significant difference in the run-time for your code.