Skip to content

Strings and Characters in Swift

Strings are one of the most commonly used data types in programming. In Swift, strings are powerful, Unicode-compliant, and come with a rich set of features for manipulation. Whether you’re displaying text to users, processing input, or working with data, understanding strings is essential.

What is a String?

A String is a series of characters, such as “Hello, World!“. In Swift, strings are represented by the String type, which is a collection of Character values.

Key Features of Swift Strings

  • Unicode-compliant - Supports all Unicode characters including emojis 😊
  • Value type - Strings are copied when assigned or passed to functions
  • Efficient - Optimized for performance with copy-on-write behavior
  • Immutable by default - Use let for constant strings
  • Rich API - Tons of built-in methods and properties

Creating Strings

String Literals

The most common way to create a string is using a string literal:

let greeting = "Hello, World!"
let name = "Swift"
let emoji = "🚀"
print(greeting) // Output: Hello, World!

Empty Strings

You can create an empty string in two ways:

// Method 1: Empty string literal
let emptyString1 = ""
// Method 2: String initializer
let emptyString2 = String()
// Check if a string is empty
if emptyString1.isEmpty {
print("String is empty") // Output: String is empty
}

Type Annotation

Swift can infer the type, but you can also be explicit:

let message: String = "This is explicitly a String"
var username: String = "JohnDoe"

Multi-line Strings

For strings that span multiple lines, use triple quotes """:

let poem = """
Roses are red,
Violets are blue,
Swift is awesome,
And so are you!
"""
print(poem)

Output:

Roses are red,
Violets are blue,
Swift is awesome,
And so are you!

Multi-line String Rules

  1. Opening quotes - """ must be on their own line
  2. Closing quotes - """ must also be on their own line
  3. Indentation - The closing """ determines the base indentation
let story = """
Once upon a time,
in a land far away,
there lived a Swift developer.
"""
// The indentation before "Once" is preserved
print(story)

Line Breaks in Multi-line Strings

Use backslash \ to break long lines without adding a line break:

let longText = """
This is a very long sentence that \
we want to split across multiple lines \
in our code, but display as one line.
"""
print(longText)
// Output: This is a very long sentence that we want to split across multiple lines in our code, but display as one line.

String Interpolation

String interpolation allows you to insert variables and expressions into strings using \(value):

let name = "Emma"
let age = 25
// Basic interpolation
let introduction = "My name is \(name) and I am \(age) years old."
print(introduction)
// Output: My name is Emma and I am 25 years old.

Interpolation with Expressions

You can include any valid Swift expression inside \():

let apples = 5
let oranges = 3
let message = "I have \(apples + oranges) fruits in total."
print(message) // Output: I have 8 fruits in total.
// With calculations
let price = 99.99
let quantity = 3
let total = "Total cost: $\(price * Double(quantity))"
print(total) // Output: Total cost: $299.97

Interpolation with Function Calls

func getCurrentYear() -> Int {
return 2024
}
let announcement = "Welcome to Swift in \(getCurrentYear())!"
print(announcement) // Output: Welcome to Swift in 2024!

String Interpolation vs Concatenation

let firstName = "John"
let lastName = "Doe"
// Using interpolation (Preferred)
let fullName1 = "\(firstName) \(lastName)"
// Using concatenation
let fullName2 = firstName + " " + lastName
// Both produce the same result
print(fullName1) // Output: John Doe
print(fullName2) // Output: John Doe

Why interpolation is better:

  • More readable and cleaner
  • Works with any type (automatically converts)
  • Less prone to errors with spacing

Characters

A Character represents a single character in Swift:

let exclamationMark: Character = "!"
let letterA: Character = "A"
let emoji: Character = "😊"
print(exclamationMark) // Output: !

Iterating Over Characters

You can loop through each character in a string:

let word = "Swift"
for character in word {
print(character)
}

Output:

S
w
i
f
t

Creating Strings from Characters

let characters: [Character] = ["S", "w", "i", "f", "t"]
let word = String(characters)
print(word) // Output: Swift

String Properties

Count

Get the number of characters in a string:

let message = "Hello, Swift!"
print(message.count) // Output: 13
let emoji = "👨‍👩‍👧‍👦"
print(emoji.count) // Output: 1 (counts as one character)

isEmpty

Check if a string is empty:

let text = ""
if text.isEmpty {
print("String is empty")
} else {
print("String has content")
}

String Methods

Uppercased and Lowercased

let message = "Hello, World!"
print(message.uppercased()) // Output: HELLO, WORLD!
print(message.lowercased()) // Output: hello, world!

Checking String Content

let greeting = "Hello, Swift!"
// Check if string starts with a substring
if greeting.hasPrefix("Hello") {
print("It's a greeting!") // This will print
}
// Check if string ends with a substring
if greeting.hasSuffix("Swift!") {
print("It's about Swift!") // This will print
}
// Check if string contains a substring
if greeting.contains("Swift") {
print("Swift is mentioned") // This will print
}

Removing Whitespace

let messyString = " Hello, World! "
// Remove leading and trailing whitespace
let cleanString = messyString.trimmingCharacters(in: .whitespaces)
print(cleanString) // Output: Hello, World!
// Remove all kinds of whitespace including newlines
let multilineMessy = " \n Hello \n "
let cleaned = multilineMessy.trimmingCharacters(in: .whitespacesAndNewlines)
print(cleaned) // Output: Hello

Replacing Substrings

let original = "Hello, World!"
// Replace a substring
let modified = original.replacingOccurrences(of: "World", with: "Swift")
print(modified) // Output: Hello, Swift!
// Multiple replacements
let text = "I love cats and cats love me"
let updated = text.replacingOccurrences(of: "cats", with: "dogs")
print(updated) // Output: I love dogs and dogs love me

Splitting Strings

let sentence = "Swift is awesome and powerful"
// Split into an array of substrings
let words = sentence.split(separator: " ")
print(words)
// Output: ["Swift", "is", "awesome", "and", "powerful"]
// Convert to array of Strings
let wordArray = sentence.split(separator: " ").map { String($0) }
print(wordArray)
// Output: ["Swift", "is", "awesome", "and", "powerful"]

Joining Strings

let words = ["Swift", "is", "amazing"]
// Join with a separator
let sentence = words.joined(separator: " ")
print(sentence) // Output: Swift is amazing
// Join without separator
let combined = words.joined()
print(combined) // Output: Swiftisamazing

String Comparison

Equality

let str1 = "Hello"
let str2 = "Hello"
let str3 = "hello"
print(str1 == str2) // Output: true
print(str1 == str3) // Output: false (case-sensitive)
// Case-insensitive comparison
print(str1.lowercased() == str3.lowercased()) // Output: true

Comparing Strings

let apple = "Apple"
let banana = "Banana"
if apple < banana {
print("Apple comes before Banana alphabetically")
}

String Concatenation

Using the + Operator

let firstName = "John"
let lastName = "Doe"
let fullName = firstName + " " + lastName
print(fullName) // Output: John Doe

Using += Operator

var message = "Hello"
message += " "
message += "World"
print(message) // Output: Hello World

Appending to Strings

var greeting = "Hello"
greeting.append(" World")
print(greeting) // Output: Hello World
// Append a character
greeting.append("!")
print(greeting) // Output: Hello World!

Escape Sequences

Special characters in strings:

// Common escape sequences
let quote = "He said, \"Hello!\"" // Double quote
let backslash = "Path: C:\\Users\\Documents" // Backslash
let newline = "First line\nSecond line" // New line
let tab = "Name:\tJohn" // Tab
print(quote) // Output: He said, "Hello!"
print(backslash) // Output: Path: C:\Users\Documents
print(newline)
// Output: First line
// Second line
print(tab) // Output: Name: John

Unicode Escape Sequences

// Using Unicode scalar values
let heart = "\u{2665}" // ♥
let smiley = "\u{1F600}" // 😀
print(heart) // Output: ♥
print(smiley) // Output: 😀

String Indexing

Swift strings use special indices (not integers) for accessing characters:

let greeting = "Hello, Swift!"
// First character
let firstChar = greeting[greeting.startIndex]
print(firstChar) // Output: H
// Last character
let lastChar = greeting[greeting.index(before: greeting.endIndex)]
print(lastChar) // Output: !
// Character at specific position
let index = greeting.index(greeting.startIndex, offsetBy: 7)
let char = greeting[index]
print(char) // Output: S

Extracting Substrings

let text = "Hello, World!"
// Get a range
let startIndex = text.index(text.startIndex, offsetBy: 7)
let endIndex = text.index(text.startIndex, offsetBy: 12)
let substring = text[startIndex..<endIndex]
print(substring) // Output: World
// Convert substring to String
let extracted = String(substring)
print(extracted) // Output: World

Practical Examples

Example 1: Validating User Input

func validateUsername(_ username: String) -> Bool {
// Username must be between 3 and 20 characters
let length = username.count
return length >= 3 && length <= 20 && !username.isEmpty
}
print(validateUsername("john")) // Output: true
print(validateUsername("jo")) // Output: false
print(validateUsername("")) // Output: false

Example 2: Building a Greeting Message

func greetUser(name: String, timeOfDay: String) -> String {
let greeting = "Good \(timeOfDay), \(name)!"
return greeting
}
let message = greetUser(name: "Emma", timeOfDay: "morning")
print(message) // Output: Good morning, Emma!

Example 3: Formatting Names

func formatName(_ fullName: String) -> String {
let trimmed = fullName.trimmingCharacters(in: .whitespaces)
let words = trimmed.split(separator: " ")
let formattedWords = words.map { word -> String in
let first = word.prefix(1).uppercased()
let rest = word.dropFirst().lowercased()
return first + rest
}
return formattedWords.joined(separator: " ")
}
print(formatName("jOHN dOE")) // Output: John Doe
print(formatName(" emma STONE ")) // Output: Emma Stone

Example 4: Counting Words

func countWords(in text: String) -> Int {
let trimmed = text.trimmingCharacters(in: .whitespacesAndNewlines)
if trimmed.isEmpty {
return 0
}
let words = trimmed.split(separator: " ")
return words.count
}
let sentence = "Swift is a powerful programming language"
print("Word count: \(countWords(in: sentence))")
// Output: Word count: 6

Example 5: Creating Initials

func getInitials(from name: String) -> String {
let words = name.split(separator: " ")
let initials = words.map { String($0.prefix(1).uppercased()) }
return initials.joined()
}
print(getInitials(from: "John Doe")) // Output: JD
print(getInitials(from: "Emma Stone")) // Output: ES
print(getInitials(from: "Robert Downey Jr")) // Output: RDJ

Common String Patterns

Check if String is Numeric

func isNumeric(_ text: String) -> Bool {
return !text.isEmpty && text.allSatisfy { $0.isNumber }
}
print(isNumeric("12345")) // Output: true
print(isNumeric("123a45")) // Output: false
print(isNumeric("")) // Output: false

Reverse a String

let text = "Swift"
let reversed = String(text.reversed())
print(reversed) // Output: tfiwS

Remove Specific Characters

let phone = "+1 (555) 123-4567"
let digitsOnly = phone.filter { $0.isNumber }
print(digitsOnly) // Output: 15551234567

String Performance Tips

  1. Use String Interpolation - More efficient than concatenation

    // Good
    let message = "Hello, \(name)!"
    // Less efficient for multiple operations
    let message2 = "Hello, " + name + "!"
  2. Avoid Repeated Concatenation in Loops

    // Inefficient
    var result = ""
    for i in 1...1000 {
    result += "\(i) "
    }
    // Better
    var parts: [String] = []
    for i in 1...1000 {
    parts.append("\(i)")
    }
    let result2 = parts.joined(separator: " ")
  3. Use isEmpty instead of count == 0

    // Good
    if text.isEmpty { }
    // Less efficient
    if text.count == 0 { }

Key Takeaways

Strings are value types - They’re copied when assigned
Use string interpolation - Cleaner than concatenation
Multi-line strings - Use """ for readability
Rich API - Lots of built-in methods for manipulation
Unicode support - Full support for emojis and international characters
Type safety - Swift ensures string operations are safe

Practice Exercises

Try these on your own to master strings:

  1. Write a function that reverses each word in a sentence
  2. Create a function that checks if a string is a palindrome
  3. Build a function that capitalizes the first letter of each sentence
  4. Write a function that removes all vowels from a string
  5. Create a function that counts the occurrences of a specific character

What’s Next?

Now that you’ve mastered Strings and Characters, you’re ready to move on to:

Up Next: Collections in Swift

  • Working with Arrays
  • Using Dictionaries
  • Understanding Sets
  • Collection iteration and manipulation

Keep practicing! Strings are fundamental to almost every program you’ll write. The more comfortable you are with string manipulation, the more efficient you’ll become as a Swift developer! 🚀