Log in to vote

How to efficiently use functions in the strings library ?

Asked by 2 years ago
Edited 2 years ago

So for the past few months, I have been grappling with functions such as string.sub, string.gsub, string.find, string.gfind, string.match,string.gmatch, and string.format, but never really understood them entirely, I have had some experience with string.sub, string.gsub, and string.find using patterns instead of the init string. For example, a couple months back, I tried making a typewriter gui using string.sub:

local text = "Some Demo Test"
local textlabel = script.parent
for i = 1, #text do
    textlabel.Text = string.sub(text,1,i)

and did some reseach on the lua users wiki,, and the roblox wiki for how to properly use these functions. Eariler today I tried experimenting with string.find and string.sub

local randomstring = "My Friend Chuck Says Hi"
print(string.sub(randomstring,2,5))--prints the 2nd, 3rd, 4th, and 5th letters of randomstring
local d = "%u+"
print(string.sub(randomstring,string.find(randomstring,d)))--prints first few uppercase letters
local d2 = "%w+"
print(string.sub(randomstring,string.find(randomstring,d2)).."---------d2")--prints the first chunk
local d3 = "%w+%s+"

but apart from those three, I really don't know much more about these functions that show up time and time again in scripts for admin commands, emotes through text, and etc. So if anyone can give me a good definition of what they do and an example of how they work, that would be much appreciated

1 answer

Log in to vote
Answered by 2 years ago
Edited 2 years ago

Alright, it's probably best to go through these one at a time. Note: The parameters I'm using aren't the officially used ones on the wiki, but it's pretty obvious what I mean and some of the wiki parameter names (s) aren't very helpful.

string.find(target, search, startPoint):

You seem to already have have a good idea of what this does, but it searches for the regular expression search in the string target and returns the starting and ending position of the first one it finds. You can use startPoint to decide where in the string to start looking. It's not (generally) particularly useful unless coupled with string.sub() somehow.

string.sub(target, start, ending):

Again, you probably know what this is, but it returns a substring of target starting at character start and ending at character ending. It's useful for trimming off part of a string that you don't need.

string.gsub(target, search, replacement):

Despite the similarity in name to string.sub, this is actually a very different function. It looks for each case of regular expression search and replaces that with replacement. It's super useful for a bunch of stuff, including removing all white-space, replacing numbers with their written-out forms, etc.


This isn't a thing on the Roblox wiki, and while it may be a thing in pure lua, should not be used in Roblox projects.

string.match(target, search, startPoint):

Parameters look pretty similar to string.find(), right? That's because it's the same thing, except it returns the string it finds instead of the starting and ending points of those strings. This can be used to make the code posted in your original post cleaner; instead of string.sub(randomstring,string.find(randomstring,d2)) you can simply do string.match(randomstring,d2). Very useful with regular expressions.

string.gmatch(target, pattern):

This returns a function you can iterate over (like pairs and ipairs). Each iteration is a string.match(target, pattern) starting off its search where the last iteration left off. Useful for looking for multiple cases of a regular expression (like all words in a sentence).*

string.format(formattingString, ...):

... represents the targets that are formatted by the formattingString. For information on what formattingString can be, check the Lua site's information on it after the sixth code-block. (I'll be honest, I don't use/understand this one much, but the site has good information on it.)

Edit: * This particular example would be done like below:

for word in string.gmatch("h0wdy pardners th1s is epi<", "[^%s]+") do -- [^%s]+ represents multiple non-space characters
Even if this answer is very old, pairs is not an iterator function, but an iterator generator function. The pairs function is literally only 1 line of code, `return next, t, nil`. User#24403 53 — 2y

Answer this question