string.len

From ComputerCraft
Jump to: navigation, search

string.len
Function
Syntax
string.len()
Colon notation: str:len()

Returns number
API string
Source Lua (source)

Return the number of bytes in str.

The number of bytes in a string is not necessarily equivalent to its amount of characters! Many Unicode characters take up more than one byte. For example, string.len("ö") returns 2. Hence, string.len only returns the amount of characters if str is encoded in an encoding that only uses one byte per character, such as ASCII.

Note Note: You can also use #str.

ExampleExamplePrint the length of user input
Read a line from the user, then print the length of the string they entered.
Code
print(string.len(read()))
Output Depends on what the user wrote. For instance, if they were to enter Hello World, the number printed would be 11.