Strings in Swift

# beginners# programming# swift# tutorial
Strings in SwiftGamya

๐ŸŒธ Swift Strings โœจ In Swift, text stored in a variable or constant is called a...

๐ŸŒธ 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 ๐ŸŒธ"
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น 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.\""
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น 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.
"""
Enter fullscreen mode Exit fullscreen mode

Swift keeps line breaks exactly as written.


๐Ÿ”น Common String Operations

Count Characters

print(animeHero.count)
Enter fullscreen mode Exit fullscreen mode

Swift correctly counts emoji and Unicode characters.

Uppercase Text

print(flower.uppercased())
Enter fullscreen mode Exit fullscreen mode

Prefix & Suffix Checks

print(poem.hasPrefix("Petals"))
print(flower.hasSuffix("๐ŸŒธ"))
Enter fullscreen mode Exit fullscreen mode

โš ๏ธ 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 ๐ŸŒธโœจ