String: Difference between revisions
mNo edit summary |
m (→Advanced) |
||
(2 intermediate revisions by the same user not shown) | |||
Line 59: | Line 59: | ||
== Advanced == | == Advanced == | ||
This will also work with [[hexadecimal]] numbers: | This will also work with [[Number|hexadecimal]] numbers: | ||
<pre> | <pre> | ||
print(0xf == 15) -- true, because 0xf is a hexadecimal number which equals 15 | 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 | print(tonumber("0xf") == 15) -- true, because you converted the string "0xf" to a number, 0xf | ||
</pre> | </pre> | ||
Line 68: | Line 68: | ||
<pre> | <pre> | ||
print(tonumber("00001111",2)) -- prints 15 | print(tonumber("00001111", 2)) -- prints 15 | ||
print(tonumber("00001111",2)==15) -- prints true | print(tonumber("00001111", 2) == 15) -- prints true | ||
print(tonumber("774",8)) -- prints 508 | print(tonumber("774", 8)) -- prints 508 | ||
print(tonumber("774",8)==508) -- prints true | print(tonumber("774", 8) == 508) -- prints true | ||
</pre> | </pre> | ||
Line 85: | Line 85: | ||
Will all result in: | Will all result in: | ||
hello | <code>hello</code> | ||
This will allow you to nest a string within another string: | This will allow you to nest a string within another string: | ||
Line 101: | Line 102: | ||
Let's have more "strings" please. | Let's have more "strings" please. | ||
</pre> | </pre> | ||
=== Multiline quotes === | === Multiline quotes === | ||
Line 117: | Line 119: | ||
<pre> | <pre> | ||
local m = Instance.new("Message") | local m = Instance.new("Message") | ||
m.Parent = | m.Parent = workspace | ||
m.Text = [[Multiple lines of text | m.Text = [[Multiple lines of text | ||
can be enclosed | can be enclosed | ||
Line 124: | Line 126: | ||
m:Remove() | m:Remove() | ||
</pre> | </pre> | ||
===Nesting quotes=== | ===Nesting quotes=== | ||
Nested quotes rely on the use of equals signs to distinguish one nested quote from another. | Nested quotes rely on the use of equals signs to distinguish one nested quote from another. | ||
Example: | Example: |
Latest revision as of 23:27, 21 September 2021
Introduction
Strings are sequences of characters, letters, numbers, letters AND numbers, symbols, and so forth. They are declared in quotes.
A script of
local x = "Hi mom" local y = "123456" local z = "Bob12345" local n = "abc!@#$%^&*()123456789" print(x) print(y) print(z) print(n)
will print in the output bar
Hi mom 123456 Bob12345 abc!@#$%^&*()123456789
Strings differ from Numbers in that you can't allocate a name like "Bob" to numbers.
Another 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.
Example:
print("5"+1)
--> 6
print("whoops"+1)
--> Cmd:1: attempt to perform arithmetic on a string value
In the first example, "5" was converted from a string to a number (notice "5" was in quotes, but 1 was not.)
In the second example "whoops" could not be converted to a number, because it was a word.
Although lua will convert strings with arithmetic (+, -, *, /), it won't automatically convert strings with comparisons. You have to convert a string to a number manually (or a number to a string) using the tonumber() or tostring() function:
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.
Advanced
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
Quotes
print "hello" print 'hello' print [[hello]] print ("hello")
Will all result in:
hello
This will allow 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.]])
Will result in:
hello "Lua user" Its [[content]] hasn't got a substring. Let's have more "strings" please.
Multiline quotes
print([[Multiple lines of text can be enclosed in double square brackets.]])
Will result in: Multiple lines of text can be enclosed in double square brackets.
An example could be:
local m = Instance.new("Message") m.Parent = workspace m.Text = [[Multiple lines of text can be enclosed in double square brackets.]] wait(5) m:Remove()
Nesting quotes
Nested quotes rely on the use of equals signs to distinguish one nested quote from another.
Example:
print([=[one [[two]] one]=]) print([===[one [[two]] one]===]) Both result in: [one [[two]] one]