How to Concatenate Strings in Swift?
In Swift, a string is a sequence of a character for example “w3wiki”, “I love w3wiki”, etc, and represented by a String type. A string can be mutable and immutable. When a string is assigned to a variable, such kind of string is known as a mutable string(can be modified) whereas when a string is assigned to a constant, such kind of string is known as an immutable string(cannot be modified). In Swift, we are allowed to concatenate strings. String Concatenation is the technique of creating a new string by adding two strings. We can perform string concatenation using the following methods:
- Using + operator
- Using += operator
- Using append() method
Now we will discuss all these methods in detail along with examples.
Using + operator
String concatenation using the + operator is the most common method of concatenation. The addition(+) operator creates a new string by adding two or more strings together. You can also use this method to concatenate multiline string literals. Let us discuss this with the help of the following example.
Syntax:
string1 + string2
Example:
Swift
// Swift program to concatenate strings import Swift // Creating strings var mystring1 = "Hello! Maya " var mystring2 = "I am a food blogger " var mystring3 = "I love eating food" // Concatenating two strings // Using + operator var concatString1 = mystring1 + mystring2 // Concatenating more strings // Using + operator var concatString2 = mystring1 + mystring2 + mystring3 // Concatenating strings // Using + operator var concatString3 = mystring1 + "I live in Mumbai" // Displaying data print ( "String 1:" , mystring1) print ( "String 2:" , mystring2) print ( "Final String 1:" , concatString1) print ( "Final String 2:" , concatString2) print ( "Final String 3:" , concatString3) |
Output:
String 1: Hello! Maya String 2: I am a food blogger Final String 1: Hello! Maya I am a food blogger Final String 2: Hello! Maya I am a food blogger I love eating food Final String 3: Hello! Maya I live in Mumbai
Using += operator
In Swift, we can add and assign a string to the existing string variable using the addition assignment operator(+=). Let us discuss this concept with the help of the following example.
Syntax:
string1 += string2
Example:
Swift
// Swift program to concatenate strings import Swift // Creating strings var mystring1 = "Hello! Maya " var mystring2 = "I am a food blogger " // Concatenating two strings // Using += operator mystring1 += mystring2 // Concatenating more strings // Using += operator mystring2 += "I love eating food" // Displaying data print ( "Final String 1:" , mystring1) print ( "Final String 2:" , mystring2) |
Output:
Final String 1: Hello! Maya I am a food blogger Final String 2: I am a food blogger I love eating food
Using append() method
In Swift, we can also concatenate strings using the append() method. This method adds a new element at the end of the specified mutable string. It does not return any value it only updates the given string.
Syntax:
string1.append(string2)
Swift
// Swift program to concatenate strings import Swift // Creating strings var mystring1 = "Hello! Maya " var mystring2 = "I am a food blogger " // Concatenating two strings // Using append() function mystring1.append(mystring2) // Displaying data print ( "Final String :" , mystring1) |
Output:
Final String : Hello! Maya I am a food blogger