How to Append a String to Another String in Swift?
Swift language supports different types of generic collections and a string is one of them. A string is a collection of alphabets. In the Swift string, we add a string value to another string. To do this task we use the append() function. This function is used to add a string value to another string. It will return the appended string.
Syntax:
string.append(str: String)
Here string is an object of the String class.
Parameters: This function accepts a parameter that is illustrated below:
- str: This is a string that will be joined to the string.
Return Value: This function returns an appended string.
Example 1:
Swift
// Swift program to add a string value to another string import Swift // Creating two strings var string1 = "Beginner" var string2 = "forBeginner" // Calling the append() function to add // above given two strings string1.append(string2) // Getting the appended string print (string1) |
Output:
w3wiki
Example 2:
Swift
// Swift program to add a string value to another string import Swift // Creating an string var string = "w3wiki" // Calling the append() functions to // add some strings string.append( " is a" ) string.append( " CS Portal." ) // Getting the appended string print (string) |
Output:
w3wiki is a CS Portal.
Contact Us