Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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!

4 comments

0

I don't think you understand. I don't know what the number will be before hand.

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

0

I cannot do that. It is being generated by a user.

1 answer

✓ Accepted answer

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

1 comment

0

Thank you so much

This discussion is preserved as it was. New answers, comments, and votes are closed.