I have a value that has a percentage (e.g. 25%).
I am trying to remove the percentage from my value so it gets converted to an int I can use for comparisons.
Here’s my code:
local percentage = string.gsub("Hello % World", "/[%]/", "")
print(percentage)
Regarding second argument, I’ve tried the following:
/[%]/
[%]
%
/%/
On the one in the code snippet above, it returns nil.
On the one with [%], it errors malformed pattern (missing ']')
On the one with %, it errors malformed pattern (ends with '%')
You can identify percentage marks with the following pattern:
string.gsub("hello % world","%%","")
I believe this information used to be on the String Patterns reference page in the dev hub, but it’s not there anymore. Basically you use a double % to capture percentage marks.
Out of curiosity, how did you end up with a situation where you need to convert a percentage string into an integer? Isn’t this backwards in what usually occurs in a program where the percentage string is made from the integer or is passed around as needed, rather than getting it from the string?
There are 12 “magic characters” which are reserved for special purposes in patterns:
[....]
Instead of using their special meaning, you can precede them with a % symbol to search for them literally.