Documentation on String functions?

Most of us have at least seen some functions for strings, not string, but a “string”. For example the function string:sub().

local String = "Hello World!"

String:sub(--[[forgot the parameters for sub()]])

I would like to know if there is any sort of documentation on all of the functions, as I can’t find any sources.

Here’s a full documentation by roblox on it: string | Roblox Creator Documentation

As I said, there are separate functions that operate on the string itself, meaning there are functions for strings, and I’m not referring to string, something that has functions, but isn’t a type of string.

Unless I’m wrong and both of these accept the same parameters and function the same way.

I don’t understand what you mean, every function is listed here:

And those are the parameters/arguments: image

I think it refers to “simplify functions for strings”, I don’t know its real name.

As I said above, it is to simplify these functions, just put a colon and the name of the function

local String = "Hello"
String:find("H")  -- true
String:sub(1,2)   -- "He"
String:len()      -- 5

and so with all the others, and when the first parameter is the string, just ignore it.

I think there is no documentation on this

Here is another example

local text = "Roblox"
print(text:sub(3, 6))

Not sure if text:sub() and string.sub(text) are the same, which is why I asked for documentation if it’s available

Yes, they are the same thing, just an abbreviation.

oh, so it’s just another way of writing them I guess

1 Like

Exactly, an abbreviation, string.sub() == OBJ:sub()

The difference, is that the OBJ takes the 1st arg, so you don’t have to put the object as the first parameter.

Oh, they are the same. String is just a example/placeholder for any type of text that people use for undefined strings (string).
local text = "Roblox", in this line, Roblox is what’s considered a string, and can be returned with string.sub. Or… i’m not understanding what you mean still if this isn’t the answer youre looking for

So, as an example:

local Text = "Hello";

print(string.sub(Text, 1, 3)) -->> "Hel".
print(Text:sub(1, 3)) --> "Hel";

The real reason that works is because strings have a metatable; try =getmetatable("") in the command bar. You should see “The metatable is locked” or something along those lines. The metatable has an __index field which points back to the string library. Knowing that a:b(...) is syntactic sugar for a.b(a, ...) (the former passes itself as first argument implicitly), now you know why it works. string.f(str, ...) is usually recommended over str:f(...) due to it performing a little better, and getting better linting for incorrect use of the functions I believe. I personally use the shortcut on non-literals, as the difference is usually negligible. And that shortcut exists, so it would be a waste to not use it. It doesn’t severely harm readability either.

1 Like