Escape string for Lua's string.format

user1593846 picture user1593846 · Aug 8, 2013 · Viewed 7.8k times · Source

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.

Answer

dunc123 picture dunc123 · Aug 8, 2013

You can escape a % with another %, e.g. string.format("%%20") will give %20