What's The True Difference Between string.find(), string.match(), [string].find(), [string].match(), [string]:find(), [string]:match()?

I know this is quite a long question, but to put it short, what is the difference between the find and match methods?

I hate stuff like this. When there are similar options, I can’t make my mind up without knowing the true difference. Please help and put my mind at ease, thank you!

Oh, and also, yes, I did do my research, but I didn’t understand what the people where talking about, in the places I looked.

12 Likes

There’s minimal difference. One is just syntactical sugar for another, similar to something like this:

local function foo()
end

-->> is the same as

local foo = function()
end

They both do exactly the same thing. They just look different.

12 Likes

Could you elaborate on that, please? – Ignore this, because you edited your reply.

So what do you suggest is better? I mean, find is at the top when you look through the list of methods, less letters too I guess.

7 Likes

Why did you delete your reply? I was just about to read that.

9 Likes

The difference is quite negligible. However, using string.match() over [string]:match() is generally better, as it’s ever so slightly faster.

9 Likes

My apologies, didn’t read your post content, only the title. So what I wrote doesn’t really make sense to the question you’re asking. I mean here if you want it:

There is small difference, the string library is just a library to manipulate strings, so even after putting a method after it, you would still have to pass the string as an argument:

local str = "Hello World!"
local Hello = string.find(str, "Hello")

print(Hello)-- Prints 1

local str2 = "Hello World!"
local Hello2 = str2.find(str2,"Hello")

print(Hello2) -- Prints 1

7 Likes

Thank you, I was going to ask on that eventually, so using the string library is better, it’s like task.wait() and wait() right?

6 Likes

Oh fair enough then, thank you for attempting to contribute anyway! :slight_smile:

6 Likes

Just to add to this, so, @VegetationBush, you would use string.match() over stirng.find()? even though its more letters?

6 Likes

Correct, but doing [said string]:find() does not require you to put in the [said string], colon operator.

5 Likes

No, they’re not really the same thing.

string.match() is a function call, while [string]:match() is a method call. Roblox has (or is going to) optimize function calling through a method called inline caching.

task.wait and wait are both from a global so this optimization doesn’t matter. Even though they’re both yielding functions, they actually have different behavior. task.wait() yields for the length of one frame, while wait() yields for 1/30th of a second.

6 Likes

Yeah you’re right, I just brought up string.find as it was probably the easiest one to make a code snippet out of. I just thought of functions like [string]:len().

My apologies, whoops :man_shrugging:

4 Likes

I’m not sure what you’re trying to say, so now you’re saying [string]:match() is better, even though earlier on you said it was slower than using the string library one.

I know, I was saying like because task.wait() is a library, just like string.

3 Likes

Ah, that’s alright, no need to apologise, thank you though! :slight_smile:

3 Likes

So then, what’s better to use?

3 Likes

I think you’re misunderstanding :sweat_smile:. To reiterate:

Note that I’m talking about optimizations with function calling, which makes it faster.

So, to answer your question of which one is “better”. The string library is faster, so it should be preferred. Although, it barely makes a difference.

4 Likes

strings in Lua/Luau just have a metatable that points towards the string library. Using the library directly is generally faster.


This also allows weird behavior like this:

print(("Hello World!").len("Hi"))
--> prints 2
-- This is reading the length of "Hi" instead of "Hello World!"

But once again, that’s because of the way metatables behave.

3 Likes

Look, uh, I’m really sorry, but i still do not understa-

Oh ok, thank you, but just to be the picky person I am. Which one? match() or find()?

3 Likes

Oh ok, I see, I suppose that actually makes a lot of sense, as doing [a piece of string]:match()/find() has to go through some stuff to then get to the library as well.

3 Likes

match and find are two different things. I was talking about using the : (colon) operator in relation to the . (dot) operator, which is what I thought you were confused about.

3 Likes