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

How to get only the Numbers in a String?

so i have this String Variable:

local string = 'Example String 256721'

i want to get only the numbers in the string (256721)
does roblox have a function for this?

1 comment

0

You mean does Lua have a function for this. Review this page https://developer.roblox.com/articles/string-patterns-reference and update your code with your attempt if you cannot get to work the way you want.

1 answer

✓ Accepted answer

For this you gotta use the string.match function, string manipulation is a really interesting thing so check it out.

string.match will return a string that contains what relates between 2 strings. For example

print(string.match("Bloxy cola", "cola"))
--this will print "cola" since that set of characters exists in the 1st string! it is found in both strings
thats why its called match

now in order to match the numbers we cant just type a number we gotta use string patterns, check these out too they are useful. now what are these you ask? they are used to represent a character, for example %d represents any digit, and thats what were going to use. %s represents spaces and so on....

local str = string.match("Example String 256721" , "%d+")
print(str)

--this will only print the numbers! and thats it, btw you also got add a **+** to the patterns.

1 comment

0

ty

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