String: Difference between revisions
>NecroBumpist slight correction, changed multiple line strings section a little. |
>NecroBumpist slight correction, changed multiple line strings section a little. |
(No difference)
|
Revision as of 15:35, 13 August 2011
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.
You can also use normal quotations with a backslash at the end of the line to create multiline strings:
local str = "line1\ line2\ line3"; print(str); Will result in: line1 line2 line3
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
Escaping
In single or double quotes, but not block quotes. You can use escapes to embed any character.
A backslash(\) followed by certain letters has special meaning (see 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" ) -- hi
Be careful if you use less than 3 numbers ("\0" for example) and a number comes after it.
print( "\000123" ~= "\0123" )
If you want to escape any of the following characters (", ', or /,) you can use the %q character class along with the string.format string function. Here's an example:
string.format(string, "%q")