DEV Community

Gamya
Gamya

Posted on Edited on

Strings in Swift

🌸 Swift Strings ✨

In Swift, text stored in a variable or constant is called a string.

Strings are written using double quotes.

let animeHero = "Hinata Hyuga"
let flower = "Cherry Blossom 🌸"
✨ อัปเดตล่าสุด: gamya m/strings in swift jgh - ประเด็นร้อน ✨ อัปเดตล่าสุด: gamya m/strings in swift jgh - ประเด็นร้อน

🔹 Quotes Inside Strings

To include quotes inside a string, escape them with a backslash:

let quote = "She said, \"Every flower blooms in its own time.\""
✨ อัปเดตล่าสุด: gamya m/strings in swift jgh - ประเด็นร้อน ✨ อัปเดตล่าสุด: gamya m/strings in swift jgh - ประเด็นร้อน

🔹 Multi-line Strings

Regular strings cannot span multiple lines.

For longer text, use triple quotes:

let poem = """
Petals fall softly,
Anime dreams rise high,
Spring never fades.
"""
✨ อัปเดตล่าสุด: gamya m/strings in swift jgh - ประเด็นร้อน ✨ อัปเดตล่าสุด: gamya m/strings in swift jgh - ประเด็นร้อน

Swift keeps line breaks exactly as written.


🔹 Common String Operations

Count Characters

print(animeHero.count)
✨ อัปเดตล่าสุด: gamya m/strings in swift jgh - ประเด็นร้อน ✨ อัปเดตล่าสุด: gamya m/strings in swift jgh - ประเด็นร้อน

Swift correctly counts emoji and Unicode characters.

Uppercase Text

print(flower.uppercased())
✨ อัปเดตล่าสุด: gamya m/strings in swift jgh - ประเด็นร้อน ✨ อัปเดตล่าสุด: gamya m/strings in swift jgh - ประเด็นร้อน

Prefix & Suffix Checks

print(poem.hasPrefix("Petals"))
print(flower.hasSuffix("🌸"))
✨ อัปเดตล่าสุด: gamya m/strings in swift jgh - ประเด็นร้อน ✨ อัปเดตล่าสุด: gamya m/strings in swift jgh - ประเด็นร้อน

⚠️ String comparisons in Swift are case-sensitive.


🌟 Wrap Up

Swift strings are safe, Unicode-aware, and powerful.

Whether it’s anime dialogue or flower names, Swift handles text beautifully 🌸✨

Top comments (0)