String: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>NXTBoy
>JulienDethurens
No edit summary
 
(16 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{CatUp|Properties}}
{{Map|Scripting|Data Types}}
__TOC__


== Introduction ==
Strings are sequences of letters, numbers, and symbols.


Strings are sequences of characters, letters, numbers, letters AND numbers, symbols, and so forth. 
== Making a string ==


A script of  
The most common method of creating strings is to put double quotes around the characters you want.
See the code below for an example:


<pre>
{{code|=
x = "Hi mom"
local str = "Hello, world!"
y = "123456"
}}
z = "Bob12345"
n = "abc!@#$%^&*()123456789"


print(x)
This will cause {{`|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.
print(y)
print(z)
print(n)
</pre>


will print in the output bar {{`|Hi mom}}, {{`|123456}}, {{`|Bob12345}}, and {{`|abc!@#$%^&*()123456789}}. [[String]]s differ from [[Number]]s in that you can't allocate a name like "Bob" to numbers. 
{{code|=
local str = "Hello, "Dave"!" -- We don't want this!
}}


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 numberIf your value can't be converted to a number, you will get an error.
How can we fix this?  There are other ways to create a string.  We can use single quotes, or we can use double brackets.


Example:
{{code and output|code=
local str0="Hello, world!"
local str1='Hello, "world"!'
local str2=[[Hello, "world"!]] <!--Syntax highlighting is incorrect for this example, but this code is correct.-->


> print("5" + 1)
print(str0)
6
print(str1)
> print ("whoops"+1)
print(str2)
<span style="color:red">Cmd:1: attempt to perform arithmetic on a string value</span>
|output=
Hello, world!
Hello, "world"!
Hello, "world"!
}}


In the first example, "5" was converted from a string to a number (notice "5" was in quotes, but 1 was not.)
== Combining strings ==
In the second example "whoops" could not be converted to a number, because it was a word.
 
Let's 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!
}}


<pre>
The process of combining two strings into one is known as ''concatenation''
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.
</pre>


===Converting a string to a number===
== 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.
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.
This is an example of tonumber() usage.
<pre>
 
{{code|=
a = "123"
a = "123"
b = 5 + tonumber(a) --tonumber() usage here
b = 5 + tonumber(a) --tonumber() usage here


print(b) --128
print(b) --128
</pre>
}}
 
== 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=
print("5" + 1)
print ("whoops"+1)
|output=
6
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.
 
{{code|=
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 ====
==== Advanced ====


This will also work with [[hexadecimal]] numbers:
This will also work with [[hexadecimal]] numbers:
<pre>
 
{{code|=
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>
}}


as well as with other based numbers, but you have to specify the base:
as well as with other based numbers, but you have to specify the base:


<pre>
{{code|=
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
Line 69: Line 105:
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>
}}


== Literals ==
== Literals ==


<pre>
{{code|=
print("hello")
print("hello")
print('hello')
print('hello')
print([[hello]])
print([[hello]])
</pre>
}}


Will all result in:
Will all result in:
hello [http://lua-users.org/wiki/StringsTutorial]
<samp>hello</samp>


This allows you to nest a string within another string:
This allows you to nest a string within another string:


<pre>
{{code and output|code=
print('hello "Lua user"')
print('hello "Lua user"')
print("Its [[content]] hasn't got a substring.")
print("Its [[content]] hasn't got a substring.")
print([[Let's have more "strings" please.]])
print([[Let's have more "strings" please.]])
</pre>
|output=
 
Will result in:
<pre>
hello "Lua user"
hello "Lua user"
Its [[content]] hasn't got a substring.
Its [[content]] hasn't got a substring.
Let's have more "strings" please.
Let's have more "strings" please.
</pre>
}}


=== Multiline Literals ===
=== Multiline literals ===


<pre>
{{code and output|code=
print([[Multiple lines of text
print([[Multiple lines of text
can be enclosed in double square
can be enclosed in double square
brackets.]])
brackets.]])
</pre>
|output=
 
Will result in:
 
  Multiple lines of text
  Multiple lines of text
  can be enclosed in double square
  can be enclosed in double square
  brackets.
  brackets.
}}


You can also use normal quotation marks with a backslash at the end of each line to create multiline strings:
You can also use normal quotation marks with a backslash at the end of each line to create multiline strings:
<pre>
 
{{code and output|code=
local str = "line1\
local str = "line1\
line2\
line2\
line3"
line3"
print(str)
print(str)
 
|output=
Will result in:
line1
line1
line2
line2
line3
line3
</pre>
}}


===Nesting square brackets===
=== Nesting square brackets ===


Nested bracketsrely on the use of equals signs to distinguish one nested bracket from another.:
Nested brackets rely on the use of equals signs to distinguish one nested bracket from another.


Example:
'''Example:'''
<pre>
{{code and output|code=
print([==[
print([==[
one [=[two]=] one
one [=[two]=] one
Line 137: Line 167:
one [=[two]=] one
one [=[two]=] one
]===])
]===])
|output=
one [=[two]=] one
one [=[two]=] one
}}


Both result in:
=== Escaping ===
one [=[two]=] one
</pre>


===Escaping===
In single or double quotes, but not block quotes, you can use backslashes ({{`|\}}) to embed any character.
In single or double quotes, but not block quotes, you can use backslashes ({{`|\}}) to embed any character.


Line 148: Line 179:


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.
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.
<pre>
{{code|=
print( 'String \'abc\'' )
print( 'String \'abc\'' )
print( "String with \" \\ and \' " )
print( "String with \" \\ and \' " )
</pre>
}}
A backslash followed by 1-3 numbers represents the string.byte value of that number.
A backslash followed by 1-3 numbers represents the string.byte value of that number.
print( "\104\105" ) -- hi
{{code and output|code=
print( "\104\105" )
|output=
hi
}}
Be careful if you use fewer than 3 numbers ("\0" for example) and a number comes after it.
Be careful if you use fewer than 3 numbers ("\0" for example) and a number comes after it.
print( "\000123" ~= "\0123" )
== See Also ==
[http://www.lua.org/pil/2.4.html Programming in Lua 2.4 -- Strings]
[http://www.lua.org/manual/5.1/manual.html Lua 5.1 Reference Manual]


[http://lua-users.org/wiki/StringsTutorial StringsTutorial]
{{code|=
print( "\000123" ~= "\0123" )
}}


[[StringValue]]
== See also ==


[[Category:Data Types]]
*[http://www.lua.org/pil/2.4.html Programming in Lua 2.4 -- Strings]
*[http://www.lua.org/manual/5.1/manual.html Lua 5.1 Reference Manual]
*[http://lua-users.org/wiki/StringsTutorial StringsTutorial]
*[[StringValue]]
*[[Category:Data types]]

Latest revision as of 05:13, 16 April 2012

Strings are 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. See the code below for an example:

local str = "Hello, world!"

This will cause 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? There are other ways to create a string. We can use single quotes, or we can use double brackets.

local str0="Hello, world!"
local str1='Hello, "world"!'
local str2=[[Hello, "world"!]] 

print(str0)
print(str1)
print(str2)

Hello, world! Hello, "world"!

Hello, "world"!

Combining strings

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

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.

print("5" + 1)
print ("whoops"+1)

6

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.

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

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.

Multiline literals

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.

Example:

print([==[
one [=[two]=] one
]=])
print([===[
one [=[two]=] one
]===])

one [=[two]=] one

one [=[two]=] one

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.

print( "\104\105" )
hi

Be careful if you use fewer than 3 numbers ("\0" for example) and a number comes after it.

print( "\000123" ~= "\0123" )

See also