Updated April 11, 2023
Introduction to Lua String Concatenation
Lua string concatenation is defined as the joining of the two strings using string operator provided by Lua programming language that is two-period operator ( . . ) which is used for joining the given strings into one string. Strings in Lua are a sequence of characters which are considered as immutable and therefore whenever the string concatenation takes place it is defined as joining of two separate string which results in a new string that contains the copy of source string into it which display as one single string as output and this process is done using two dots between the strings to concatenate them into one single string and it will not modify any operands as strings are immutable.
Syntax:
In Lua concatenation of string is done using string operator two dots or two periods such as ( . . ) so the syntax is as shown below:
local variable_result= variable_str1 . . variable_str2
In the above syntax, we can see the variable to store the concatenated string is declared using “local” that is variable_result and the strings that need to be concatenated are stored in variable_str1 and variable_str2 respectively. Then we can see we are using (. .) two-dot operator which is a string operator for joining or concatenating two strings into one string where this two-dot operator must be placed between the strings that are to be concatenated,
How String Concatenation works in Lua?
In Lua, string concatenation is a very simple process as in other programming languages where the string concatenation is performed using two dot operators ( . . ) or is also known as a two-period operator which is a string operator used for concatenating the strings.
The exact working of string concatenation is as follows firstly the string that needs to be concatenated are declared in separate variables with a different name or we can directly use the strings and the two-dot operator to concatenate. Then the resulted string is stored in another separate variable that contains only one string. Therefore, the first string is followed by the two-dot operator ( . .), and this operator is followed by another string that needs to be joined to the first string. Then the resulting string is concatenated string which is displayed as one single string after applying the two-dot operator. Now let us demonstrate this in the below example in Lua programming code.
Examples
Let us discuss examples of Lua String Concatenation.
Example #1
Code:
print("Demonstration of string concatenation using Lua")
print("\n")
local first_str = "Educba"
print("The first string is given as follows: ")
print(first_str)
print("\n")
local second_str = "Institute"
print("The second strig is given as follows: ")
print(second_str)
print("\n")
print("The resulting concatenated string is as follows: ")
local result_str = first_str .. " " .. second_str
print(result_str)
Output:
In the above program, we can see that we have first declared two strings using local with variable name as “first_str” and “second_str” and we are printing the strings that are declared and then we can see we have declared another variable named “result_str” to store the concatenated string and the concatenation is done using two dot operator (. .) between the strings and here it is done as first_str . . “ ” . . second_str which means the first_str (Educba) is concatenated with space and this space is in turn concatenated with second_str (Institute). So we can see in the above program output which then displays “Educba Institute” as output in the above screenshot. we are using space for better reading of the output in a similar way we can use other special character or string also for concatenating instead of space between the string operator and the string for concatenation.
Now let us see another example below where we can use string concatenation directly without declaring any string.
Example #2
Code:
print("Demonstration of string concatenation without separate string declaration using Lua")
print("\n")
local first_str = "Educba"
print("The first string is given as follows: ")
print(first_str)
print("\n")
local second_str = "Institute"
print("The second strig is given as follows: ")
print(second_str)
print("\n")
print("The resulting concatenated string is as follows: ")
local result_str = first_str .. " " .. second_str .. "," .. " " .. "India"
print( result_str)
Output:
In the above program, we can see the first two strings we have declared but in the output, we can see we have printed 3 strings. So we can see when we are concatenating in the above program it is done as we are concatenating first string followed by two-dot operator followed by space then again two-dot operator followed by second string then two dot operator followed by “,” and space and lastly we can see the third string is just directly declared using double quotes (“ ”) which look like “India”. The output of the above program is as seen in the above screenshot.
We should note that in any programming language and also in Lua programming the string is always declared in either double quotes or single quotes. In the Lua programming language, as strings are considered as immutable the Lua will not alter or change the operands that are declared during concatenation and the output will result in the single string without affecting the strings.
Conclusion
In this article, we conclude that string concatenation in the Lua programming language is done in a very simple way as in other programming languages. In this, we can see the concatenation means joining of the strings that are we are appending one string with another string using string operator known as two dot operator ( . .) between the string for concatenating.
Recommended Articles
We hope that this EDUCBA information on “Lua String Concatenation” was beneficial to you. You can view EDUCBA’s recommended articles for more information.