Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I take out a comma from a string and make it only as one number?

Asked by 4 years ago

I can't figure out how to take out a comma from a string and make it only as one number.

print(string.match('Cost: $2,501', '(%d+)%z?,%z?(%d+)'))
-- Output: 2 501 -- It gives two numbers instead of just one.

2 answers

Log in to vote
1
Answered by
TrippyV 314 Donator Moderation Voter
4 years ago
Edited 4 years ago

It's most likely something string.match does automatically when separating sets. You can simplify the string pattern you're using by using gmatch:

local str = ""

for i in ("Cost: $2,501"):gmatch("%d+") do
    str = str .. i
end

print(str)
0
Thank you! UltraUnitMode 419 — 4y
Ad
Log in to vote
1
Answered by
kisty1 111
4 years ago

You could replace the comma using gsub and then match for any digits. Like so

print(string.match(string.gsub("Cost: $2,501", ",", ""), "%d+"))
0
Thanks so much! UltraUnitMode 419 — 4y

Answer this question