Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

escaping formatting characters in java String.format

Tags:

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.

like image 531
Omar Kooheji Avatar asked Oct 14 '08 14:10

Omar Kooheji


People also ask

How do you escape special characters in a string in Java?

Java Special Characters The solution to avoid this problem, is to use the backslash escape character.

How do you escape a formatted string?

Answers. You can use a backslash to escape a formatting character: String. Format("{0:Save #\\%}", 100);

How do you escape a character in a string?

\ 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 \ .

How do you escape the escape character in Java?

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 ("").


1 Answers

You can just double up the %

like image 148
Draemon Avatar answered Oct 12 '22 23:10

Draemon