DEV Community

Gamya
Gamya

Posted on Edited 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)
✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน ✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน

Output:

Naruto Uzumaki
✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน ✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน

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)
✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน ✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน

Output:

Luffy wants to become the King of the Pirates
✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน ✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน

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)
✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน ✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน

Output:

15
✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน ✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน

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)
✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน ✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน

Output:

One Piece!!!
✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน ✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน

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)
✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน ✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน

Output:

Goku Ultra Instinct
✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน ✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน

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)
✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน ✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน

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)
✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน ✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน

Output:

I rate Attack on Titan 10/10.
✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน ✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน

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)
✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน ✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน

Output:

Kakashi completed 1200 missions.
✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน ✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน

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
✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน ✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน

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)
✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน ✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน

But interpolation is much easier:

let text = "Episode \(episode)"
print(text)
✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน ✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน

Output:

Episode 5
✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน ✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน

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.")
✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน ✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน

Output:

Gojo used 30% of his power.
✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน ✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน

You can even do more advanced calculations:

let chakra = 80
let boost = 20

print("Final chakra level: \(chakra + boost)")
✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน ✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน

Output:

Final chakra level: 100
✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน ✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน

🔹 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)
✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน ✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน

Output:

Levi said "Keep moving forward."
✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน ✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน

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)
✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน ✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน

Output:

Tanjiro fought Akaza in 3 epic battles!
✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน ✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน

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"
✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน ✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน

Using String Interpolation

let text = "Hello \(name)"
✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน ✨ อัปเดตล่าสุด: gamya m/how to join strings together in swift llm - ประเด็นร้อน

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)