java – How to escape % in String.Format?

java – How to escape % in String.Format?

To escape %, you will need to double it up: %%.

To complement the previous stated solution, use:

str = str.replace(%, %%);

java – How to escape % in String.Format?

This is a stronger regex replace that wont replace %% that are already doubled in the input.

str = str.replaceAll((?:[^%]|\A)%(?:[^%]|\z), %%);

Leave a Reply

Your email address will not be published. Required fields are marked *