string.sub
sub is short for substring.
Part of a string is a substring.
The parameter names I used aren't the ones in the wiki, they weren't very descriptive so I made my own
string.sub(target, starting, ending) will return substring from string target starting at starting and ending at ending. Note that you don't need to provide ending, it will default to the length of the string if you don't need it.
Examples
print(string.sub("Hello", 2, 4));
print(string.sub("World", 1));
print(string.sub("foo bar", 2, 4));
If starting is negative, it will start from the end of the string rather than the start, so string.sub("Hello", -1) would return o because it is the last character.
print(string.sub("Lua", -1))
print(string.sub("strings", -2))
ending can be negative aswell. Here are examples;
print(string.sub("dogs", -2, -1));
Here is a little function that gets a player from a partial string:
local function getPlayerFromPartial(s)
s = s:lower()
for _, client in ipairs(Players:GetPlayers()) do
if client.Name:sub(1, #s):lower() == s then
return client
end
end
return nil
end
It traverses the table returned by Players:GetPlayers, checks the name of each player, lowercases it, gets the substring at 1, #s (#s is the length of the string, # is the unary length operator, though you should know already), and if there is a match, return the player. The function will return nil if no match.
3 comments
ok i dont understand this at all, only the prints parts but not the function part
Which part should I make clearer? What specifically do you not understand ?
i dont understand the local function part and how i would fit it into player.Chatted function