I have a string I need to add a variable to so I use the string.format
method, but the string also contains the symbol "%20"
(not sure what it represents, probably a white space or something). Anyway since the string contains multiple "%"
and I only want to add the variable to the first one to set the id, is there a way to escape the string at the points or something?
As it is now:
ID = 12345
string.format("id=%s&x=foo=&bar=asd%20yolo%20123-1512", ID)
I get bad argument #3 to 'format' (no value).
error -- since it expects 3 variables to be passed.
You can use a backslash to escape a formatting character: String. Format("{0:Save #\\%}", 100);
In the platform, the backslash character ( \ ) is used to escape values within strings. The character following the escaping character is treated as a string literal.
Escaping a character means the character will be treated as part of the string, rather than a Lua instruction. To escape a character, place a \ in front of it, like so: message = "he said \"bye\" and left"print (message) ...
C# includes escaping character \ (backslash) before these special characters to include in a string. Use backslash \ before double quotes and some special characters such as \,\n,\r,\t, etc. to include it in a string.
You can escape a %
with another %
, e.g. string.format("%%20")
will give %20
Unlike many other languages, Lua uses %
to escape the following magic characters:
( ) . % + - * ? [ ] ^ $
The code below escape all URL escapes (that is, % followed by a digit):
ID=12345
f="id=%s&x=foo=&bar=asd%20yolo%20123-1512"
f=f:gsub("%%%d","%%%1")
print(string.format(f,ID))
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