How to keep only specific characters in a string? Or an alternative to tonumber()?

I have a text box where players can type whatever and press a button to submit it. That’s the context but basically I want to take the numbers and ONLY the numbers from the string. Now I could use tonumber() but the problem is that it doesn’t remove every character, and I want ONLY the number characters. If someone submits something like 128.78 I want to receive 12878, not a decimal. Or if somebody submits 24e3 I’ll get 2400 back but I want 243. So I turned my head to gsub, but that wouldn’t work because I’d have to remove every possible character except for numbers and that would just be tedious

maybe string.match can work, but im not sure.

You can use

string.find(string, %d) -- %d represents all digits, should return a table afaik

more info

As it turns out, I can use string.gsub to get rid of any characters that affect the output of tonumber. Works like a charm

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.