String

String is a type that represents an immutable sequence of characters.

This article is about the type. For the API, see String API. For the crafting ingredient, see Printed Pages.

Literal value

Strings can be used in a program's source code by using a string literal, which is typically indicated by enclosing the desired sequence of characters within a pair of single or double quotes:

<nowiki>
-- myString and myOtherString are variables that hold a string value
local myString = "Hello world!"
local myOtherString = 'Hola mundo!'
</nowiki>

Character escaping

To use characters such as a newline (\n), the delimiter used to open/close the literal (" or '), or to specify specific characters by their numeric code (e.g. 20 for space), such characters must be prepended with a backslash, and to use a backslash literally, escape it with another backslash:

<nowiki>
local escapedString = "John: \"Hey There!\"\nAlex: \\Timidly\\ \"H h.. hi\""
print(escapedString)
--[[
The following will be printed:

John: "Hey There!"
Alex: \Timidly\ "H h.. hi"
]]

local ydots = "\255"
print(ydots) -- ÿ
</nowiki>
Valid escapes
Code Character
\a Bell
\b Back space
\f Form feed
\n Newline
\r Carriage return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\" Double quote
\' Single quote

Multiline literals

The above method of expressing a string literal is for single-line usage only, for a multi-line string literal, begin the sequence with [[ and terminate it with ]]:

<nowiki>
local multilineString = [[
Hello world!
I span multiple lines!]]
</nowiki>

One thing to note is that when using the multiline literal, Lua will ignore the first newline if nothing follows the initial [[, but not the trailing ]]:

<nowiki>
local multilineString = [[
abc
def]]

print(multilineString == "abc\ndef") -- true


local secondMultiline = [[
abc
def
]]

print(secondMultiline == "abc\ndef\n") -- true
</nowiki>

Additionally, inside of a multiline string, there is no concept of escapes, so using characters like " and \ will represent themselves instead of acting specially:

<nowiki>
local multilineString = [[
John: "Hey There!"
Alex: \Timidly\ "H h.. hi"]]

print(multilineString) -- Prints exactly what you would expect

print([[\255]]) -- Will print "\255" instead of "ÿ"
</nowiki>

Therefore, to include the [[ symbol, place = symbols between the brackets, like so:

<nowiki>
local myString = [=[
Hello world!
This [[can contain]] double square brackets
and still [be[[valid]]]
]=]

local myOtherString = [===[
You can nest these literals by using more ='s
Testing [=[ Hello! [[]] ]=]
like so ^^^^^^^^^^^^^^^^^^^
]===]
</nowiki>

There is no limit to the amount of ='s one can place between the brackets, thereby allowing for deeply nested multline string literals, if desired.

gollark: I guess it could be modified to stay on until 50% heat, but that would be mostly unhelpful except during very high but brief demand (which is why we have actual buffers) and also would be unsafe.
gollark: I don't know if the current control system could read the comparator as analog and stay turned on for longer, but it wouldn't help outside of weird circumstances.
gollark: We have stable power. It just can't scale to above 2.7kRF/t or whatever.
gollark: That reminds me; we could make an automatic scrap processor for free random junk.
gollark: The plates/wires are especially annoying.
This article is issued from Computercraft. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.