How to iterate through a string and find a specific number

I am using a for loop to iterate through a folder and get there respective names, that folder has some objects. For example,

Folder

  • Object1
  • Object2
  • Object3

My problem is I want to identify them by not making 3 individual if statements. So, I want to identify them with there numbers at the end of the string and How am I suppose to do that?

You’re still going to need 3 if statements.
you can use string.find

1 Like

I’ll try doing that see if that works. I don’t know about string.find so I might have to learn that at first.

for i,v in pairs(folder) do
    local number = tonumber(string.gsub(v, "%D", ""))
end

%D selects all character that are not a number and replaces it with a blank string

2 Likes

Yep, I’ll try learning this too. I have to know how this works. Thanks for the help too.

i dunno what u r trying to do.But I wrote this code.Does this matches with what u r trying to do…Also am a Bangladeshi

local folder = script.Parent.Folder
for i, v in pairs(folder:GetChildren()) do
	v.Name = "Folder"..i
	print(v.Name)
end

1 Like

I think this isn’t working. Folder isn’t a file that is available :confused:

1 Like

I think this should work…

local folder = script.Parent.Folder
for i, v in pairs(folder:GetChildren()) do
	if v.Name == "Folder"..i then
		--if the last number matches do something--
		--- remeber one thing  "i" changes..it's like i = 1,2,3
	end

end

Oh, I think that’s not what I meant. Thanks for helping btw. I meant was to iterate more then 3 objects but there are 3 identical objects which I want to find by making a for loop to understand what numbers they got.

1 Like

the value of “i” is not limited , it changes with how many objects u have.if u have 5, the value of i
will be 1,2,3,4,5…
it’s like this. i hope u can understand this.

1 Like

I know this already so This isn’t the thing I am going for. It’s much more complex. I want to iterate a sting to be honest .

I think I got it. Thanks for helping.