How do I concatenate strings in Swift?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Swift gives you several straightforward ways to concatenate strings. The most common are the + operator, +=, append, and string interpolation. The best choice depends on whether you are joining a few known strings, building text incrementally, or embedding values into a sentence.
Using the + operator
The simplest approach is +.
This is clear and fine for small fixed combinations.
Using string interpolation
If you are building human-readable text, interpolation is often more readable.
Interpolation also works well with numbers and expressions.
For user-facing strings, interpolation is often the cleanest style.
Using +=
If you are mutating an existing string, += is convenient.
This is readable for simple incremental construction.
Using append
Swift strings also support append.
This is particularly nice when a string is being built step by step inside conditional logic and you want the mutation to read explicitly rather than through repeated binary concatenation expressions.
This is especially useful when the code already works with a mutable String and wants to add one more piece without introducing extra intermediate expressions.
Joining many strings
If you need to combine many pieces, especially from an array, use joined(separator:).
This is better than chaining many + operations when the values already live in a collection.
Which style to use
A practical rule is:
Swift supports several valid styles, so the real question is usually not “which one exists,” but “which one makes the intent of this particular string-building code easiest to read.”
- use
+for a few simple string pieces - use interpolation for readable sentences with values
- use
+=orappendwhen building a mutable string step by step - use
joined(separator:)for arrays of strings
Swift supports all of them, so choose based on readability rather than treating one as universally correct.
Performance perspective
For ordinary app code, readability matters more than micro-optimizing tiny string joins. If you are building very large strings inside tight loops, prefer patterns that reflect incremental construction clearly and avoid awkward repeated concatenation patterns.
In most Swift app code, the better choice is simply the one that makes the produced string obvious to the next developer reading the function.
In normal UIKit or SwiftUI code, the difference is usually far less important than keeping the result easy to read.
Common Pitfalls
A common mistake is overusing + for long user-facing messages when interpolation would be clearer.
That kind of code still works, but it tends to hide the final shape of the string instead of making it obvious at a glance.
Another mistake is manually joining array elements with loops when joined(separator:) already expresses the intent directly.
A third mistake is forgetting that only mutable strings can use += or append.
Summary
- Use
+for simple direct concatenation. - Use interpolation for readable strings with embedded values.
- Use
+=orappendwhen building a mutable string incrementally. - Use
joined(separator:)for arrays of strings. - Choose the style that makes the resulting code easiest to read.
- Prefer interpolation when building natural-language output with embedded values.
Related reading
- How do I configure full URLs in xcconfig files
- How do I connect to a specific Wi-Fi network in Android programmatically?
- How do I convert a Swift Array to a String?
- How do I convert a Swift Array to a String?
- How do I convert an NSString value to NSData?
- How do I convert an NSString value to NSData?
- How do I correctly detect orientation change using Phonegap on iOS?
- How do I create a basic UIButton programmatically?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.