String

From Goodblox Wiki
Revision as of 10:52, 23 June 2020 by Pizzaboxer (talk | contribs) (Created page with "{{CatUp|Properties}} __TOC__ == Introduction == Strings are sequences of characters, letters, numbers, letters AND numbers, symbols, and so forth. A script of <pre> x =...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Introduction

Strings are sequences of characters, letters, numbers, letters AND numbers, symbols, and so forth.

A script of

x = "Hi mom"
y = "123456"
z = "Bob12345"
n = "abc!@#$%^&*()123456789"

print (x)
print (y)
print (z)
print (n)

will print in the output bar Hi mom, 123456, Bob12345, and 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 [1]

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 = game.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]

See Also

Programming in Lua 2.4 -- Strings

Lua 5.1 Reference Manual

StringsTutorial

StringValue