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

Is there a way to check if a string has a number in it and if so to return it?

Asked by 5 years ago

The title says it all. How can I check and see if there is an integer inside of a string and if there is to return that number? Thanks for any help!

0
I don't think you understand. I don't know what the number will be before hand. User#21908 42 — 5y
0
I didn't know what you meant by return, go to the script where the number is being generated in the first place and print it greatneil80 2647 — 5y
0
I cannot do that. It is being generated by a user. User#21908 42 — 5y

1 answer

Log in to vote
2
Answered by
mattscy 3725 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

You are able to use string patterns to isolate numbers from a string. The %d character class represents any digit, and adding the + represents that you want to retrieve any number of digits next to each other, so you can use string.match with this info to retrieve numbers from the string:

local function GetNumbers(String)
    return string.match(String,"%d+")
end

print(GetNumbers("banana 63 banana")) --> 63
0
Thank you so much User#21908 42 — 5y
Ad

Answer this question