Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

The Difference between len and # (operator)? In Roblox aofcourse!

I tried to do this

string.len("hi") and strin = "hi" print(#strin)

They both printed two but what I wanted to clarify is if they're exactly the same or a bit different because some thing that are same or similar tend to be a bit different you know what I mean hozei!!

5 comments

1

they are essentially the same but you can check the length of a table with #

0

Will the # (operator) also return nil as a result or not ? In a table of course!!!

0

nope

0

Use #. len probably is there because there wasn't a proper length operator, but idk

0

I was curious myself so I tested them both in a speed test, string.len() is significantly less efficient so I would use #string instead

1 answer

Hello, xxcoordinatorxx!

string.len(string) and print(#string) don't really have a big difference, the only one that I can remember is that # can be used to Tables. I'll give some examples...

local string = "A text"

print(string.len(string)) -- This will print '6'

print(#string) -- This will print '6'

however,

local string = {"Nice", "Explanation", "Leamir", 12348, true}

print(string.len(string)) -- This will print nil, as "string" is now not a string, but a table

print(#string) -- This will print 5, as the table "string" have 5 indexes

It is better use # for strings and tables, as it works for both

Any errors please post a comment and I'll solve

Good Luck with your games

2 comments

1

No, you should use # on strings.

0

Uhh... Normally you use .length or :len() on strings. However, # is a little more powerful as it gets the length of strings and tables.

This discussion is preserved as it was. New answers, comments, and votes are closed.