String: Difference between revisions

From Goodblox Wiki
Jump to navigationJump to search
(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 =...")
 
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{CatUp|Properties}}
{{CatUp|Data Types}}
__TOC__
__TOC__


== Introduction ==
== Introduction ==


Strings are sequences of characters, letters, numbers, letters AND numbers, symbols, and so forth.
Strings are sequences of characters, letters, numbers, letters AND numbers, symbols, and so forth. They are declared in quotes.


A script of


A script of
<pre>
<pre>
x = "Hi mom"
local x = "Hi mom"
y = "123456"
local y = "123456"
z = "Bob12345"
local z = "Bob12345"
n = "abc!@#$%^&*()123456789"
local n = "abc!@#$%^&*()123456789"


print (x)
print(x)
print (y)
print(y)
print (z)
print(z)
print (n)
print(n)
</pre>
</pre>
will print in the output bar
<pre>
Hi mom
123456
Bob12345
abc!@#$%^&*()123456789
</pre>


will print in the output bar Hi mom, 123456, Bob12345, and abc!@#$%^&*()123456789 .  <b>Strings</b> differ from [[Numbers]] in that you can't allocate a name like "Bob" to numbers.   
<b>Strings</b> differ from [[Number|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.
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)<br>
'''Example:'''
6<br>
 
print ("whoops"+1)<br>
<code>print("5"+1)</code> --> <code>6</code>
<font color="red">Cmd:1: attempt to perform arithmetic on a string value</font>
 
<code>print("whoops"+1)</code> --> <code><font color="red">Cmd:1: attempt to perform arithmetic on a string value</font></code>
 


In the first example, "5" was converted from a string to a number (notice "5" was in quotes, but 1 was not.)
In the first example, "5" was converted from a string to a number (notice "5" was in quotes, but 1 was not.)
Line 37: Line 46:


<pre>
<pre>
print("50" == 50) -- false, because a string is not equal to a number.
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(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(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.
</pre>
</pre>
[[Category:Data Types]]


== 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 55: 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 72: Line 85:


Will all result in:
Will all result in:
hello [http://lua-users.org/wiki/StringsTutorial]
<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 88: Line 102:
Let's have more "strings" please.
Let's have more "strings" please.
</pre>
</pre>


=== Multiline quotes ===
=== Multiline quotes ===
Line 104: Line 119:
<pre>
<pre>
local m = Instance.new("Message")  
local m = Instance.new("Message")  
m.Parent = game.Workspace
m.Parent = workspace
m.Text = [[Multiple lines of text  
m.Text = [[Multiple lines of text  
can be enclosed  
can be enclosed  
Line 111: 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:
Line 135: Line 151:
[[StringValue]]
[[StringValue]]


[[Category:Properties]]
[[Category:Data Types]]

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]

See Also

Programming in Lua 2.4 -- Strings

Lua 5.1 Reference Manual

StringsTutorial

StringValue