Open In App

How to Append a String to Another String in Swift?

Last Updated : 05 Jul, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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 = "Geeks"
var string2 = "forGeeks"

// Calling the append() function to add
// above given two strings
string1.append(string2)

// Getting the appended string
print(string1)

Output:

GeeksforGeeks

Example 2:

Swift
// Swift program to add a string value to another string
import Swift

// Creating an string
var string = "GeeksforGeeks"

// Calling the append() functions to 
// add some strings
string.append(" is a")
string.append(" CS Portal.")

// Getting the appended string
print(string)

Output:

GeeksforGeeks is a CS Portal.

Next Article
Article Tags :

Similar Reads