Confused about string.format and string.match

Hello!

Sorry if this has been asked before but I simply do not understand string.format. I’ve tried looking around and have seen people provide format codes and whatnot but I don’t understand how it works and why it is more efficient than :gsub or just creating my own function. It just looks like a bunch of gibberish to me when people throw around random strings, I just don’t get it.

Same thing with :match, why is it more efficient than using string.find?

I took a read at this but I still don’t get why it’s more efficient and more reliable than creating my own?


With string.format you can format a string with patterns, basically another way of combining.

local test1 = 'test'
local test2 = 1234

local str = 'Hello, this is a %s! %d'
local formatted = string.format(str, test1, test2)

print(formatted) --> Hello, this is a test! 1234

I don’t really know the difference between string.match and string.find, but I usually go with string.find for stuff such as search bars for a few reasons.


1 Like