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

How to Dissect Numbers From Strings?

I was wondering how I could have a number separated from a string through script, Here's an example:

Word = "ROBLOX122"
print numbersof(Word) --Output would return "122"

3 answers

✓ Accepted answer

So what I do here its creating function named what you want you can change that if you want but don't forgot to change the name when you calling your function too. Assert check if the type of the parametre taken is a string, after we're creating 2 table, 1 to got all of characters in the string and the other for returning the numbers.After, yeah after again, we're now taking all of letter in the text and insert it in the table named as > "String"> , then we're looking in the letter if the letter is a number and insert it in the table named as >"number"> else it does nothing.Finally we returning the table >number> and we're getting all numbers.

function findNumbersInString(text)
	assert(type(text) == "string", "The parametre given should be a string !")
	local String, number = {}, {};
	local position = 0;
	for i = 1, #text do
		String[i] = string.sub(text, i, i);
	end
	for i = 1, #text do
		if tonumber(String[i]) then
			table.insert(number, position + 1, String[i])
			position = position + 1
		end
	end
	return table.concat(number, '')
end

local txt = "Hello152"
print(findNumbersInString(txt))

First of all, you would need to create a function that returns the numbers of this, which i assume you'd already known.

Anyways, let's get straight to the code. To do this, we'll need to set up a table of numbers, and we'll pick through the word to remove the letters. Try this:

function numbersof(Text)
	local Digits={"0","1","2","3","4","5","6","7","8","9"}
	local NumbersInText="" --Sets up string to return
	for i=1,Text:len() do
		local Valid=false --Sets up variable to decide if to keep the substring
		for _,Digit in pairs(Digits) do
			if Text:sub(i,i)==Digit then --Checks if the substring is a digit
				Valid=true
			end
		end
		if Valid==true then
			NumbersInText=NumbersInText..Text:sub(i,i) --Adds to total string to return
		end
	end
	return NumbersInText
end

Word = "ROBLOX122"
print(numbersof(Word))

Anyways, I hope this makes sense. I've tested it out and it works fine for me.


If you have any further questions/problems then please leave a comment below. Hope I helped :P

####-Dyler3

3 comments

0

+1 point for you ;)

0

Thanks, i'll return the favor :P

0

Oh thanks :D ^.^

Based on the comments below (thanks so much for them, by the way!), I've decided to fix my answer;

example = "3Roblox122" -- Your string.
pattern = "%d+" Finds numerics in string

AllNumbers = {} -- Creates a table for you to store all of the numbers in

for match in example:gmatch(pattern) do -- Finds all matches in the pattern "%d+"
    table.insert(AllNumbers, #AllNumbers+1, match) -- Inserts these values into a table
end

print(table.concat(AllNumbers, "")) -- prints them all out side-by-side and in order.

Again, thanks so much for helping me fix this answer :).

2 comments

0

Its not work if Word = "3Roblox122" Its will only print 3.

0

This would work if you went ahead and did a loop.

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