Question about functions that return more than one value

string.find(str) returns the indices of str where the occurrence starts and ends, so my question is, how do I make f not be nil in here:

local str = "rbxassetid://607948062"
local s, f = string.find(str, "rbx://") or string.find(str, "rbxassetid://")
print(s, f) -- f will always be nil

I know why f is nil, but I can’t find a clean way to do it.

1 Like
local str = "rbxassetid://607948062"
local s, _ = string.find(str, "rbx://") or string.find(str, "rbxassetid://")
print(s)

The whole point is that i want f, I dont have a use for s

local str = "rbxassetid://607948062"
local _, f= string.find(str, "rbx://") or string.find(str, "rbxassetid://")
print(f)

This only returns “1” no matter how hard i try? (I can’t find a second argument.)

Otherwise:

local str = "rbxassetid://607948062"
local s, f = string.find(str, "rbx://") or string.find(str, "rbxassetid://")

local new = string.split(s, "%p")
print(new)
print(new[1], new[2])

This is the best I can do

local str = "rbxassetid://607948062"
local s, f = string.find(str, "rbx://", 1, true)
if not f then
	s, f = string.find(str, "rbxassetid://", 1, true)
end
print(s, f)

You have a third string call there, I am not sure what it does but it’s not needed.

the “new” would be a table with all the strings seperated in it

for example “hello world” would be “hello”, “world” (aka new[1] and new[2])
if that makes any sense

even the code you sent in the start does not return 2 strings for me…

Of course it wont return two strings, returns the indices of str where the occurrence starts and ends

And f here will always be nil no matter what you do, that’s because it’s assigning the first value to s and ignoring f

It’s the same as doing

local str = "rbxassetid://607948062"
local s = string.find(str, "rbx://") or string.find(str, "rbxassetid://")
local f = nil
print(s, f) -- f will always be nil

So the best way I found is this:

It doesn’t look elegante but it gets the job done.

When i print “s” it prints “1”…?

But hey if your solution works then its fine

1 Like

Yes, because that string we are looking for starts at “1”.
Read up on how string works then hopefully you’ll understand

local str = "rbxassetid://607948062"
local s, f = string.find(str, "rbxassetid://") or string.find(str, "rbx://")

Juz interchange them

That’s not a solution, the whole point of using or is to go to the second option if the first one is nil, you just switched them cause you know what str is.

You say that but you also said:

Just “1” is only a start, Not an end

EDIT: I’m saying if s was “1 13” it my method would make 1, and 13 seperate.

Well then u shudn’t stress over this so much, there is a way to do it rite? Its such a simple issue, so u shudn’t neglect it over a matter as silly as clean code.

local str = "Nerbzzz"
-------------^^^^^^^
-------------1234567
local startIndex, endIndex = string.find(str, "zzz")

print(startIndex) -- This prints 5
print(endIndex) -- This prints 7

I hope that clears up any confusion.

Most languages return -1 if a requested item is not found, but since Lua returns nil on a failed search, that method is probably the most efficient way to do it.

local subject = "rbxa?s?s?e?t?i?d?://"
local str = "rbxassetid://607948062"
local str2 = "rbx://607948062"
local s, f = string.find(str, subject)
local s2, f2 = string.find(str2, subject)
print(s, f) --1 13
print(s2, f2) --1 6

You can use the ? modifier which searches for 0 or 1 occurrences of a specified character/search pattern.