![]() |
In Swift, a string is a sequence of a character for example “GeeksforGeeks”, “I love GeeksforGeeks”, 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:
Now we will discuss all these methods in detail along with examples. Using + operatorString 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:
Example: Swift
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 += operatorIn 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:
Example: Swift
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() methodIn 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:
Swift
Output: Final String : Hello! Maya I am a food blogger |
Reffered: https://www.geeksforgeeks.org
How To |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |