Append or Concatenate Strings in Swift
To concatenate two strings in Swift, use the concatenation operator +. The operator creates and returns a new string containing the characters from both operands.
var str = str1 + str2
Here, str1 and str2 are the strings being joined. The resulting concatenated string is assigned to the variable str.
Swift also provides the += operator and the append() method when you need to add text to an existing variable. String interpolation is often the clearest option when combining strings with values such as integers or Boolean values.
Concatenate Two Strings Using the Swift + Operator
In the following program, two strings stored in str1 and str2 are concatenated using the + operator.
main.swift
var str1 = "Hello"
var str2 = " World!"
var str = str1 + str2
print( str )
Output
Hello World!
The leading space in str2 separates Hello from World!. Swift does not automatically insert spaces while concatenating strings.
Concatenate Two or More Strings in Swift
You can concatenate more than two strings in a single expression. Add explicit separator strings such as " " wherever spaces are required.
main.swift
var str1 = "Swift"
var str2 = "Tutorial"
var str3 = "by"
var str4 = "TutorialKart."
//concatenate strings
var str = str1 + " " + str2 + " " + str3 + " " + str4
print( str )
Output
Swift Tutorial by TutorialKart.
Append Text to a Swift String with +=
Use the += operator when you want to add another string to an existing variable. The variable must be declared with var because its value is modified.
var message = "Hello"
message += " World!"
print(message)
Output
Hello World!
The statement message += " World!" is a concise form of assigning message + " World!" back to message.
Append a String or Character with append()
Swift’s append() method modifies a string by adding another string or a single character at the end.
var greeting = "Hello"
greeting.append(" Swift")
greeting.append("!")
print(greeting)
Output
Hello Swift!
Use append() when the intent is specifically to extend an existing string. As with +=, the target must be a variable rather than a constant.
Combine Strings and Values with Swift String Interpolation
String interpolation is useful when a string contains variables or values of different types. Place an expression inside \(...) within a string literal.
let language = "Swift"
let version = 6
let message = "Learning \(language) version \(version)"
print(message)
Output
Learning Swift version 6
Interpolation avoids manually converting non-string values before concatenation and is usually easier to read than a long chain of + operators.
Join an Array of Swift Strings with a Separator
When the strings are stored in an array, use joined(separator:). This method inserts the specified separator between each element without adding it before the first element or after the last one.
let words = ["Swift", "String", "Concatenation"]
let result = words.joined(separator: " ")
print(result)
Output
Swift String Concatenation
Swift String Concatenation with Empty Strings
Concatenating an empty string does not change the visible text. This can be useful when a component is conditionally included.
let prefix = ""
let name = "Taylor"
let result = prefix + name
print(result)
Output
Taylor
Choosing a Swift String Concatenation Method
- Use
+to create a new string from a small number of strings. - Use
+=to add text to an existing string variable. - Use
append()when explicitly appending a string or character. - Use string interpolation for readable strings containing variables or non-string values.
- Use
joined(separator:)when combining an array of strings.
Common Swift String Concatenation Questions
Does Swift add spaces when concatenating strings?
No. You must include spaces in one of the strings or concatenate a separate space such as " ".
Can a Swift let constant be changed with += or append()?
No. A value declared with let cannot be modified after initialization. Declare the string with var when it needs to be extended.
How do you concatenate a String and an Int in Swift?
Use string interpolation, such as "Count: \(count)", or explicitly convert the integer with String(count) before using the + operator.
What is the best way to concatenate many Swift strings?
If the values are stored in an array, use joined(separator:). For incrementally built text, update a variable with += or append().
Conclusion
In this Swift Tutorial, we concatenated strings using the + and += operators, appended text with append(), combined values with string interpolation, and joined arrays of strings with a separator.
TutorialKart.com