Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape % in String.Format?

I am storing a SQL query in my strings.xml file and I want to use String.Format to build the final string in code. The SELECT statement uses a like, something like this:

SELECT Field1, Field2 FROM mytable WHERE Field1 LIKE '%something%' 

In order to format that I replace 'something' with %1$s so it becomes:

SELECT Field1, Field2 FROM mytable WHERE Field1 LIKE \'%%1$s%\' 

I escape the single quotes with the backslash. However I am not able to escape the % sign.

How can I include a like statement in my strings.xml file?

like image 289
Matthew Avatar asked Feb 16 '11 02:02

Matthew


People also ask

How do you escape a formatted string?

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

How do I escape a character in a string?

To insert characters that are illegal in a string, use an escape character. An escape character is a backslash \ followed by the character you want to insert.

How do you escape a string format in Java?

If you want to escape percent sign in String's format method, you can use % twice ( %% ). As you can see, we have used %% to escape percent symbol in 10% .

How do you escape a string format in Python?

You can use the backslash (\) escape character to add single or double quotation marks in the python string format. The \n escape sequence is used to insert a new line without hitting the enter or return key. The part of the string after \n escape sequence appears in the next line.


1 Answers

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

like image 74
limc Avatar answered Sep 21 '22 05:09

limc