I want to do something like this:
SQL.Text := Format('select foo from bar where baz like ''%s%''',[SearchTerm]);
But Format doesn't like that last '%', of course. So how can I escape it? \%
? %%
?
Or do I have to do this:
SQL.Text := Format('select foo from bar where baz like ''%s''',[SearchTerm+'%']);
?
%% will escape the % sign and print it as part of the output. %n will print out a new line character.
String's format() method uses percent sign( % ) as prefix of format specifier. For example: To use number in String's format() method, we use %d , but what if you actually want to use percent sign in the String. If you want to escape percent sign in String's format method, you can use % twice ( %% ).
To obtain the character "%" in a format string, enter "%%", e.g. printf("The percentage is %d %%. *n",5); yields The percentage is 5 %. Most users will find that "%g" is the best choice for printing out floating point numbers.
To selectively escape percent (%) in Python strings, we put % before the % sign to escape it. to escape the first % with %% . Then we print the string, one % will be printed.
Use another % in the format string:
SQL.Text := Format('select foo from bar where baz like ''%s%%''',[SearchTerm]);
%% , IIRC.
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