Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 6 years ago

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!!

1
they are essentially the same but you can check the length of a table with # theking48989987 2147 — 6y
0
Will the # (operator) also return nil as a result or not ? In a table of course!!! xxcoordinatorxx 135 — 6y
0
nope Leamir 3138 — 6y
0
Use #. len probably is there because there wasn't a proper length operator, but idk User#19524 175 — 6y
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 Vulkarin 581 — 6y

1 answer

Log in to vote
0
Answered by
Leamir 3138 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

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

1
No, you should use # on strings. User#19524 175 — 6y
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. M39a9am3R 3210 — 6y
Ad

Answer this question