How come strings don't have a length field?

In languages like c# for example strings have a length property

string Bro = "Bruh moment";
Console.WriteLine(Bro.Length); // 11

So i am wondering why Lua strings don’t. Of course I could use the # operator but strings are immutable so I don’t see the point in an operator for strings

They have a length method though: print(("hey"):len())

1 Like

Lua strings do, there are 2 ways actually, using string.len function or the :len() function,

local myString = "hi"
print(string.len("hi")) -- expected output: "2"
print(string.len(myString)) -- expected output: "2"
print(("hi"):len()) -- expected output: "2"
print(myString:len()) -- expected output: "2"

They’re both the same function. When you use :len() you are calling it as a method of a particular string object. (sort of like passing the string as a ‘self’ argument)

Strings do have a length method. You can use #, string.len, or :len() like the others suggested.

print(#"abcedfgjokwaeqgojwre")
print(string.len("yes"))
print(("12345"):len())

except that string.len is from a library while :len() is a method, but they both work

My bad, I guess I meant that they both have the same functionality.

@Blokav @FastKnight401 a function call to get a strings length is pointless. The length of a string will never change so I shouldn’t have to use an operator or a function for something that is constant

What exactly do you need from us? Am I responsible for writing the Lua string library?

This is true for strings you make and store in scripts, but if the user made the string and you need to find the length, then you would have to use an operator.

No just wondering, since c# has this and Java, as well as a few more. So I would like to know why.

I guess it’s just the way strings are implemented. I do know that in other languages (e.g. Java) strings do have a length property as well as a length method, the latter of which most posts above describe, but I don’t think there’s any real reason for that. Want to ask Roberto Ierusalimschy? :wink:


Fascinating page I came across while searching, though this is just length functions and probably not relevant at all:

Because it doesn’t. I think the only reason the Lua developers didn’t add it was because they didn’t think it would be a good contribution.

Just because I’m bored though…

If you some how unlock the string metatable you could do this where you overwrite the __index method of the default string metatable. (Yes this works for anyone wondering, just run the code in an unsandboxed lua environment like https://www.lua.org/demo.html)

local stringMetatable = getmetatable("")
local oldIndex = stringMetatable.__index

function stringMetatable:__index(index)
    if index == "length" then
        return #self
    end

    return oldIndex[index]
end

Or in a more plausible (and equally dumb) case you could wrap your string objects in a table or userdata.

local String = {}

do
    --// String Class Methods

    local stringMethods = {
        __metatable = "Sorry I am locked!!"
    }

    stringMethods.__index = stringMethods

    function String.new(string)
        local newString = {
            length = #string,
            value = string
        }

        setmetatable(newString, stringMethods)

        return newString
    end
end

local testString = String.new("hello world")

print(testString.length)

However all of this is silly and you should just use the string::len function.

3 Likes