DEV Community

Gamya
Gamya

Posted on

How to Join Strings Together in Swift

Swift gives us two powerful ways to combine strings together:

  • Using the + operator
  • Using string interpolation

Both are useful, but string interpolation is usually the cleaner and more efficient option.

If you’re building iOS apps, games, or even anime-themed projects, you’ll use string joining constantly for messages, UI text, scores, usernames, and more 🌸✨


🔹 Joining Strings with +

The simplest way to combine strings is using +.

let firstName = "Naruto"
let lastName = "Uzumaki"

let fullName = firstName + " " + lastName
print(fullName)
Enter fullscreen mode Exit fullscreen mode

Output:

Naruto Uzumaki
Enter fullscreen mode Exit fullscreen mode

Swift takes both strings and joins them into one brand-new string.


🔹 Joining Multiple Strings

You can chain many strings together if needed.

let hero = "Luffy"
let dream = "King of the Pirates"

let intro = hero + " wants to become the " + dream
print(intro)
Enter fullscreen mode Exit fullscreen mode

Output:

Luffy wants to become the King of the Pirates
Enter fullscreen mode Exit fullscreen mode

This works perfectly for small examples and quick text combinations.


🔹 Operator Overloading in Swift

Earlier, you learned that + adds numbers:

let result = 10 + 5
print(result)
Enter fullscreen mode Exit fullscreen mode

Output:

15
Enter fullscreen mode Exit fullscreen mode

But with strings, + means “join together.”

This is called operator overloading — the same operator can do different things depending on the data type.

For example:

let anime = "One Piece"
let excitement = "!!!"

print(anime + excitement)
Enter fullscreen mode Exit fullscreen mode

Output:

One Piece!!!
Enter fullscreen mode Exit fullscreen mode

Swift understands that both values are strings, so it joins them instead of adding numbers.


🔹 Using +=

Swift also provides +=, which appends text directly to a string.

var powerLevel = "Goku"

powerLevel += " Ultra Instinct"
print(powerLevel)
Enter fullscreen mode Exit fullscreen mode

Output:

Goku Ultra Instinct
Enter fullscreen mode Exit fullscreen mode

This is useful when building strings step by step.


🔹 Why Too Much + Can Be Wasteful

Using + repeatedly creates temporary strings behind the scenes.

Look at this:

let code = "D" + "E" + "M" + "O"
print(code)
Enter fullscreen mode Exit fullscreen mode

Swift actually builds several temporary strings:

  1. "D" + "E""DE"
  2. "DE" + "M""DEM"
  3. "DEM" + "O""DEMO"

Those temporary strings are discarded afterward.

For tiny examples this is fine, but in real apps with lots of text processing, it becomes inefficient.

That’s where string interpolation shines ✨


🔹 String Interpolation

String interpolation lets you place variables directly inside strings using \().

This is the preferred Swift way to build strings.

let anime = "Attack on Titan"
let rating = 10

let message = "I rate \(anime) \(rating)/10."
print(message)
Enter fullscreen mode Exit fullscreen mode

Output:

I rate Attack on Titan 10/10.
Enter fullscreen mode Exit fullscreen mode

Swift automatically converts values into strings for you.


🔹 Why String Interpolation Is Better

With interpolation:

✅ Cleaner code
✅ Easier to read
✅ Faster and more efficient
✅ Works with many data types

For example:

let ninja = "Kakashi"
let missions = 1200

let summary = "\(ninja) completed \(missions) missions."
print(summary)
Enter fullscreen mode Exit fullscreen mode

Output:

Kakashi completed 1200 missions.
Enter fullscreen mode Exit fullscreen mode

Notice how missions is an integer, yet Swift inserts it into the string automatically.


🔹 The Problem with + and Numbers

This code does not work:

let episode = 5

let text = "Episode " + episode
Enter fullscreen mode Exit fullscreen mode

Swift gives an error because you can’t directly join a string and an integer using +.

You could convert the number manually:

let text = "Episode " + String(episode)
print(text)
Enter fullscreen mode Exit fullscreen mode

But interpolation is much easier:

let text = "Episode \(episode)"
print(text)
Enter fullscreen mode Exit fullscreen mode

Output:

Episode 5
Enter fullscreen mode Exit fullscreen mode

Cleaner and simpler 🌸


🔹 Calculations Inside Strings

One awesome feature of string interpolation is that you can place calculations directly inside \().

print("Gojo used \(3 * 10)% of his power.")
Enter fullscreen mode Exit fullscreen mode

Output:

Gojo used 30% of his power.
Enter fullscreen mode Exit fullscreen mode

You can even do more advanced calculations:

let chakra = 80
let boost = 20

print("Final chakra level: \(chakra + boost)")
Enter fullscreen mode Exit fullscreen mode

Output:

Final chakra level: 100
Enter fullscreen mode Exit fullscreen mode

🔹 Quotes Inside Strings

Sometimes you need quotation marks inside a string.

Use a backslash before quotes:

let quote = "Levi said \"Keep moving forward.\""
print(quote)
Enter fullscreen mode Exit fullscreen mode

Output:

Levi said "Keep moving forward."
Enter fullscreen mode Exit fullscreen mode

This is called an escape sequence.


Here’s a fun example combining everything together:

let hero = "Tanjiro"
let enemy = "Akaza"
let totalBattles = 3

let story = "\(hero) fought \(enemy) in \(totalBattles) epic battles!"
print(story)
Enter fullscreen mode Exit fullscreen mode

Output:

Tanjiro fought Akaza in 3 epic battles!
Enter fullscreen mode Exit fullscreen mode

This is exactly the kind of string building you’ll use in real iOS apps and games.


🌟 Wrap Up

Swift provides two ways to join strings together:

Using +

let text = "Hello " + "World"
Enter fullscreen mode Exit fullscreen mode

Using String Interpolation

let text = "Hello \(name)"
Enter fullscreen mode Exit fullscreen mode

Although both work, string interpolation is usually the best choice because it’s cleaner, easier to read, and more efficient.

Strings are everywhere in app development—chat apps, games, anime trackers, login screens, and more. ✨

Top comments (0)