String API

From ComputerCraft
Jump to: navigation, search
FunctionFunctionstring.byte
Return the byte of the first character in the given str. As string literals in Lua represent ASCII strings, this will most likely return ASCII codepoints. To convert a byte back into a string, use string.char.
Syntax string.byte()
Colon notation: str:byte()
Returns number
Part of Lua (source)
API string
ExampleExamplePrint the bytes of the user input
Read a character from the user, then print the byte of each letter, separated by spaces.
Code
for character in read():gmatch(".") do
  write(string.byte(character))
  write(" ")
end
Output Depends on what the user wrote. For instance, if they were to enter Hello World, the output would be 72 101 108 108 111 32 87 111 114 108 100.


FunctionFunctionstring.char
Creates a string containing only the byte num. Hence, the number passed must be at least 0 and at most 255, and will be floored to the nearest integer.
Syntax string.char()
Returns string
Part of Lua (source)
API string
ExampleExamplePrint the decoded character of the byte entered in the user's input
Read a line from the user, then prints the character that the entered codepoint represents.
Code
print(string.char(tonumber(read()))
Output Depends on what the user wrote. For instance, if they were to enter 65, the character printed would be A.

String.dump

FunctionFunctionstring.find
Finds a pattern in a sentence.
Syntax string.find()
Colon notation: sentence:find()
Returns number
Part of Lua (source)
API string
ExampleExampleFind the word hello
Returns the start and end pos of the word.
Code
string.find("hello world","hello")
Output 1 5
ExampleExampleFind the word hello with start at pos 2
Returns nil, because it starts at pos 2.
Code
string.find("hello world","hello",2)
Output nil
ExampleExampleFind the letter "l" in the last 6 letters
Returns the start and end pos of the letter "l" in the word world.
Code
string.find("hello world","l",-6)
Output 10 10
ExampleExampleFinding without pattern
Normaly it would find only the letter "l". When you turn the pattern finding off, it finds the letter "%" as well.
Code
string.find("%l","%l",1,true)
Output 1 2

String.format String.gmatch

FunctionFunctionstring.gsub
Replaces a string in a sentence.
Syntax string.gsub()
Colon notation: sentence:gsub()
Returns string
Part of Lua (source)
API string
ExampleExampleReplace all letter "l"
All letters "l" have been replaced by the letter "L".
Code
string.gsub("hello world","l","L")
Output heLLo worLd
ExampleExampleLimit the substitutions
It limits the substitutions to the first 2 letters found
Code
string.gsub("hello world","l","L",2)
Output heLLo world


FunctionFunctionstring.len
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.
Syntax string.len()
Colon notation: str:len()
Returns number
Part of Lua (source)
API string
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.


FunctionFunctionstring.lower
Return a modified version of str where all uppercase characters have been converted to lowercase. Special characters and numbers are not affected.
Syntax string.lower()
Colon notation: str:lower()
Returns string
Part of Lua (source)
API string
ExampleExamplePrint the user's input in all lowercase.
Read a line from the user, then print string in all lowercase.
Code
print(string.lower(read()))
Output Depends on what the user wrote. For instance, if they were to enter Hello World, the string printed would be hello world.

String.match

FunctionFunctionstring.rep
Return the given str repeated repeat times.
Syntax string.rep()
Colon notation: str:rep()
Returns string
Part of Lua (source)
API string
ExampleExampleRepeat user input 3 times
Read a line from the user, then print the the same line repeated 3 times.
Code
print(string.rep(read(), 3))
Output Depends on what the user wrote. For instance, if they were to enter Hello World, the output would be Hello WorldHello WorldHello World.


FunctionFunctionstring.reverse
Return the given str with the character order reversed.
Syntax string.reverse()
Colon notation: str:reverse()
Returns string
Part of Lua (source)
API string
ExampleExamplePrint the reverse of user input
Read a line from the user, then print the same input reversed.
Code
print(string.reverse(read()))
Output Depends on what the user wrote. For instance, if they were to enter Hello World, the output would be dlroW olleH.


FunctionFunctionstring.sub
Return a string including the characters from start to end within str inclusively. If the arguments are negative, the character count will wrap the other way around. If the last argument end is not given, the substring will end at the end of the initial string.
Syntax string.sub()
Colon notation: str:sub()
Returns string
Part of Lua (source)
API string
ExampleExampleStrip the first and last characters from users input
Read a line from the user, then print the same string without the first and last characters
Code
print(string.sub(read(), 2, -2))
Output Depends on what the user wrote. For instance, if they were to enter Hello World, the string printed would be ello Worl.
ExampleExampleReturn ending of a string
Read a line from the user, then return the last 7 characters if the string.
Code
print(string.sub(read(), -7))
Output Depends on what the user wrote. For instance, if they were to enter Hello World, the string printed would be o World.


FunctionFunctionstring.upper
Return a modified version of str where all lowercase characters have been converted to uppercase. Special characters and numbers are not affected.
Syntax string.upper()
Colon notation: str:upper()
Returns string
Part of Lua (source)
API string
ExampleExamplePrint the user's input in all uppercase.
Read a line from the user, then print string in all uppercase.
Code
print(string.upper(read()))
Output Depends on what the user wrote. For instance, if they were to enter Hello World, the string printed would be HELLO WORLD.