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?
To escape % , you will need to double it up: %% .
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.
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% .
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.
To escape %
, you will need to double it up: %%
.
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