String: Difference between revisions
>Builder1010101 Changed the <pre> formats to <code lua> and to the code and output formats. |
>Sduke524 This really wasn't a good beginners tutorial. I just tried to add some stuff to where it would be easier for beginners to understand. |
||
Line 2: | Line 2: | ||
{{CatUp|Properties}} | {{CatUp|Properties}} | ||
Strings are sequences of characters, letters, numbers, letters AND numbers, symbols, and so forth. | |||
__TOC__ | __TOC__ | ||
== Making a String == | |||
The most common way to make a new string is to use the parenthesis, ( "" ) and whatever is inside them will be your new string. | |||
<code lua> | |||
local str="Hello, world!" | |||
</code> | |||
will make variable {{`|str}} the equivalent of the string {{`|Hello, world}}. However this isn't the only way to make a string. | |||
{{code and output|code= | |||
local str0="Hello, world!" | |||
local str1='Hello, world!' | |||
local str2=[[Hello, world!]] | |||
print(str0) | |||
print(str1) | |||
print(str2) | |||
|output= | |||
Hello, world! | |||
Hello, world! | |||
Hello, world! | |||
}} | |||
== Combining strings == | |||
Lets say you wanted to combine two strings into one. This can be easily done by using ( .. ) in between the two strings. | |||
{{code and output|code= | |||
local str0="Hello," | |||
local str1=" world!" | |||
local str2=str0 .. str1 | |||
print(str0) | |||
print(str1) | |||
print(str2) | |||
|output= | |||
Hello, | |||
world! | |||
Hello, world! | |||
}} | |||
The process of combining two strings into one like so is known as ''concatenation'' | |||
==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. | |||
<code lua> | <code lua> | ||
a = "123" | |||
b = 5 + tonumber(a) --tonumber() usage here | |||
print( | print(b) --128 | ||
</code> | </code> | ||
== 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. | |||
{{code and output|code= | {{code and output|code= | ||
Line 42: | Line 78: | ||
print(tonumber("50") == 50) -- true, because you converted the string "50" to a number | 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. | print(50 .. "" == "50") -- true, because you tacked on an empty string to the end of the number 50, converting 50 to a string. | ||
</code> | </code> | ||
Revision as of 21:42, 29 January 2012
Strings are sequences of characters, letters, numbers, letters AND numbers, symbols, and so forth.
Making a String
The most common way to make a new string is to use the parenthesis, ( "" ) and whatever is inside them will be your new string.
local str="Hello, world!"
will make variable str the equivalent of the string Hello, world. However this isn't the only way to make a string.
Combining strings
Lets say you wanted to combine two strings into one. This can be easily done by using ( .. ) in between the two strings.
The process of combining two strings into one like so is known as concatenation
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.
a = "123"
b = 5 + tonumber(a) --tonumber() usage here
print(b) --128
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.
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.
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
Literals
print("hello")
print('hello')
print(hello)
Will all result in: hello [1]
This allows you to nest a string within another string:
Multiline Literals
You can also use normal quotation marks with a backslash at the end of each line to create multiline strings:
Nesting square brackets
Nested brackets rely on the use of equals signs to distinguish one nested bracket from another.
Example:
Escaping
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.
Be careful if you use fewer than 3 numbers ("\0" for example) and a number comes after it.
print( "\000123" ~= "\0123" )