What is the best method to get the length of a string: # operator or string.len?

I’m doing some string manipulation and I need a method of getting the length of the string. So I used the # operator. Later on, when browsing the API documentation of the string methods, I found this method called string.len. It gives this description:

Receives a string and returns its length.

That means it has the same usage as the # operator, right? So here is my question:

  • What are the advantages of string.len over the # operator (and vice-versa)?

Looking forward to your answers!!

# is objectively the best method. It is idiomatic, and even works with tables (specifically array part) as well. So you don’t need to switch between string.len and table.getn, just use one operator :slight_smile:

Edit: thanks to @AwesomePossum212 for reminding me to clarify that it’s for the array part only.

2 Likes