This question is pretty much the same as this .Net question exept for java.
How do you escape the %1$
characters in a java String.format
?
THe reason I need to do this is that I'm building up a string that will later have more info inserted into it. I've thought of having one of the args just be "%1$"
but that doesn't seem to be very elegant?
sorry if this is obvious my java is a tad rusty.
Java Special Characters The solution to avoid this problem, is to use the backslash escape character.
Answers. You can use a backslash to escape a formatting character: String. Format("{0:Save #\\%}", 100);
\ is a special character within a string used for escaping. "\" does now work because it is escaping the second " . To get a literal \ you need to escape it using \ .
In Java, if a character is preceded by a backslash (\) is known as Java escape sequence or escape characters. It may include letters, numerals, punctuations, etc. Remember that escape characters must be enclosed in quotation marks ("").
You can just double up the %
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With