Swift String append()

The append() method adds a string value to another existing string.

Example

var msg1 = "Swift"
var msg2 = "Programming"

// append str1 and str2 msg1.append(msg2)
print(msg1) // Output: SwiftProgramming

append() Syntax

The syntax of the string append() method is:

string.append(str: String)

Here, string is an object of the String class.


append() Parameters

The append() method takes a single parameter.

  • str - string to be joined to string

append() Return Value

  • returns the appended string.

Example: Swift string append()

var greet = "Hello, "

// append new string to greet greet.append("Good Morning") // append "!" to greet greet.append("!")
print(greet) // Output: Hello, Good Morning!

Using + Operator to Append

In Swift, we can also use the + operator to append two strings. For example,

var greet1 = "Hello, "
var greet2 = "Good Morning"

// use + operator to append print(greet1 + greet2)
// Output: Hello, Good Morning
Did you find this article helpful?