Strings are an important component of scripting on Roblox, as they are used to represent sequences of letters, numbers, and symbols.
Making a string
The most common method of creating strings is to put double quotes around the characters you want.
local str = "Hello, world!"
This will cause the variable
str to contain the string
Hello, world. However, what if you wanted to have double quotes within your string? If you have double quotes in a string, it will cause unwanted effects.
local str = "Hello, "Dave"!" -- We don't want this!
How can we fix this? Well, there are other ways to create a string!
We can use single quotes, or we can use double brackets!
Let’s say you wanted to combine two strings into one. This can be easily done by using ( … ) in between the two strings.
local str0="Hello," local str1=" world!" local str2=str0 .. str1 print(str0) print(str1) print(str2)
Hello, world! Hello, world!
The process of combining two strings into one is known as concatenation. You can do this by using
.. in between what you want to combine.
Converting a string to a number
You can easily convert a string to a number by using the tonumber() function. This function takes one argument, which is a string, and will return the string into a number. The string must be a sequence of characters that resembles a number, such as “5128”, “2”, etc. Any strings that don’t resemble numbers, such as “Hello”, will return nil.
This is an example of tonumber() usage.
local a = "123" local b = 5 + tonumber(a) --tonumber() usage here print(b)
Math and strings
An important note with strings is that if you try to perform arithmetic on a string value, it will try to convert the string to a number. If your value can’t be converted to a number, you will get an error.
print("5" + 1) print("whoops"+1)
The first line will print
"5" can be converted to a number. The string
"whoops" however, cannot be converted to a number, and so the following error will be thrown:
print("whoops"+1):2: attempt to perform arithmetic on a string value
Here are a few more examples of number to string conversions, and what you are allowed to do with them:
print("50" == 50) -- false, because a string is not equal to a number. print(tostring(50) == "50") -- true, because you converted the number 50 to a string print(tonumber("50") == 50) -- true, because you converted the string "50" to a number print(50 .. "" == "50") -- true, because you tacked on an empty string to the end of the number 50, converting 50 to a string.
This will also work with hexadecimal numbers:
print(0xf == 15) -- true, because 0xf is a hexadecimal number which equals 15 print(tonumber("0xf") == 15) -- true, because you converted the string "0xf" to a number, 0xf
as well as with other based numbers, but you have to specify the base:
print(tonumber("00001111",2)) -- prints 15 print(tonumber("00001111",2)==15) -- prints true print(tonumber("774",8)) -- prints 508 print(tonumber("774",8)==508) -- prints true
print("hello") print(hello) print([[hello]])
Will all result in:
This allows you to nest a string within another string:
print(hello "Lua user") print("Its "content" hasn't got a substring.") print("Let's have more \"strings\" please.")
hello "Lua user" Its "content" hasn't got a substring. Let's have more "strings" please.
print([[Multiple lines of text can be enclosed in double square brackets.]])
Multiple lines of text can be enclosed in double square brackets.
You can also use normal quotation marks with a backslash at the end of each line to create multiline strings:
local str = "line1\ line2\ line3" print(str)
line1 line2 line3
Nesting square brackets
Nested brackets rely on the use of equals signs to distinguish one nested bracket from another.
print([==[ one [=[two]=] one ]==]) print([===[ one [=[two]=] one ]===])
one [=[two]=] one one [=[two]=] one
In single or double quotes, but not block quotes, you can use backslashes (
\) to embed any character.
Backslash followed by certain letters have special meanings (see the Lua Manual).
A backslash followed by punctuation or newline overrides any special meaning and puts the character in the string. This is most useful in put quotes or backslash into a quoted string.
print( String \abc\ ) print( "String with \" \\ and \ " )
A backslash followed by 1-3 numbers represents the string.byte value of that number.
print( "\104\105" )
InstructionsOutput the following string to the console:
"There’s not much time, he observed" [[Page 13]].
print("\"\There's not much time\, he observed\" [[Page 13]].")