Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Delphi 7, how do I escape a percent sign (%) in the Format function?

Tags:

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+'%']); 

?

like image 593
Blorgbeard Avatar asked Nov 06 '08 02:11

Blorgbeard


People also ask

How do you escape the percent sign?

%% will escape the % sign and print it as part of the output. %n will print out a new line character.

How do you write a percent sign in string format?

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

How do I get printf percentage?

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.

How do you escape a percent sign in Python?

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.


2 Answers

Use another % in the format string:

SQL.Text := Format('select foo from bar where baz like ''%s%%''',[SearchTerm]); 
like image 162
Robert Gamble Avatar answered Oct 18 '22 12:10

Robert Gamble


%% , IIRC.

like image 26
moobaa Avatar answered Oct 18 '22 12:10

moobaa