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?

Your builder path starts here. Builders don't just know how to code, they create solutions that matter.

Escape tutorial hell and ship real projects.

Try Programiz PRO
  • Real-World Projects
  • On-Demand Learning
  • AI Mentor
  • Builder Community