Concatenation: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Camoy
for examples; formatted; categorized
>Nightname
No edit summary
 
(13 intermediate revisions by 5 users not shown)
Line 1: Line 1:
==Concatenation==
To quote Wikipedia:
===What is Concatenation?===
<blockquote cite="https://en.wikipedia.org/w/index.php?title=Concatenation&oldid=483997444">
Concatenation is the operation of joining two strings together.
In computer programming, string concatenation is the operation of joining two character strings end-to-end. For example, the strings "snow" and "ball" may be concatenated to give "snowball".</blockquote>


===Use Cases===
The string concatenation operator in Lua is denoted by two dots (..). Here is an example of concatenation that concatenates "snow" and "ball" :
{{Example|<pre>
{{Code and output|
print("Hello" .. " Lua User")
|code=print("snow" .. "ball")
|output=snowball
}}
That code will concatenate "snow" and "ball" and will print the result, <samp>snowball</samp>.


Output:
Numbers can be concatenated to strings, too. In this case they are [[coercion|coerced]] into strings and then concatenated. For example, suppose you wanted to concatenate "Green bottles: " and 10:
Hello Lua User


local who = "Lua User"
{{code and output
print("Hello "..who)
|code=print("Green bottles: " .. 10)
|output=Green bottles: 10
}}


Output:
Hello Lua User
</pre>}}


Numbers can be concatenated to strings. In this case they are [[Coercion|coerced]] into strings and then concatenated.
=== See also ===
 
*[[Function_Dump/Table_Manipulation#table.concat_.28table_.5B.2C_sep_.5B.2C_i_.5B.2C_j.5D.5D.5D.29|table.concat]]
{{Example|<pre>
*[http://lua-users.org/wiki/StringsTutorial Concatenation]
print("Green bottles: "..10)
*[https://en.wikipedia.org/wiki/Concatenation Wikipedia: Concatenation]
 
Output:
Green bottles: 10
 
print(type("Green bottles: "..10))
 
Output:
string
</pre>}}


[[Category:Scripting_Tutorials]]
[[Category:Scripting_Tutorials]]
{{EmphasisBox|Strings concatenated onto numbers must have a space after the number of else they won't be coerced.|orange|dark=true}}
{{Example|<pre>
print("There are "..10.." green bottles.")
Output:
malformed number near '10..'
print("There are "..10 .." green bottles.")
Output:
There are 10 green bottles.
</pre>}}
=== See Also ===
[http://lua-users.org/wiki/StringsTutorial Concatenation]

Latest revision as of 18:32, 28 March 2012

To quote Wikipedia:

In computer programming, string concatenation is the operation of joining two character strings end-to-end. For example, the strings "snow" and "ball" may be concatenated to give "snowball".

The string concatenation operator in Lua is denoted by two dots (..). Here is an example of concatenation that concatenates "snow" and "ball" :

print("snow" .. "ball")
snowball

That code will concatenate "snow" and "ball" and will print the result, snowball.

Numbers can be concatenated to strings, too. In this case they are coerced into strings and then concatenated. For example, suppose you wanted to concatenate "Green bottles: " and 10:

print("Green bottles: " .. 10)
Green bottles: 10


See also