Chapter 30: Strings

1. What is a String in Swift? (the most important big picture)

A String is an ordered collection of characters — basically, text.

But Swift strings are smarter and safer than in many other languages:

  • They support full Unicode (emoji, Hindi, Telugu, Arabic, Chinese, Japanese, flags, skin tones…)
  • They count human-visible characters (not bytes or code points)
  • Strings are value types → when you assign or pass them, a copy is made
  • Strings are immutable when declared with let → you cannot change their content
  • Strings are mutable when declared with var → you can append, replace, remove…
Swift

2. Creating Strings – all the common ways you’ll actually use

Swift

Quick tip: Use raw strings (#”…# ) whenever you have backslashes (\) or quotes (“) — it saves you from writing lots of \\ and \”.

3. Most Important String Properties & Methods (you will use these 100 times)

Swift

4. Looping over characters – very common & very useful

Swift

Important things to understand:

  • You loop over grapheme clusters (what humans see as one character)
  • “😊”.count == 1
  • “नमस्ते”.count == 6 (even though it looks like 6 characters)

This is different from many older languages that count bytes or Unicode scalars.

5. String Interpolation – the modern, clean way to build text

Swift

Advanced interpolation (very popular 2024–2026 style)

Swift

6. Working with parts of a string (substrings, prefix, suffix)

Swift

Modern & safe way (avoid force-unwrap)

Swift

7. Very Common Real-Life Examples (you will write these)

Example 1 – Clean username validation

Swift

Example 2 – Simple phone number formatting

Swift

Example 3 – Password strength indicator

Swift

8. Quick Summary – Most Important String Operations

Goal Best modern code example Notes / Tip
Build text with variables “Hi \(name), age: \(age)” Interpolation – #1 method
Multi-line text “”” … “”” Cleanest way
Length text.count Counts visible characters (emoji = 1)
Empty check text.isEmpty Fast & clear
Upper / lower text.uppercased(), text.lowercased() Use for case-insensitive comparison
Contains substring text.contains(“word”) Simple & fast
Replace text text.replacingOccurrences(of: “old”, with: “new”) Returns new string
Trim whitespace text.trimmingCharacters(in: .whitespacesAndNewlines) Clean user input
Loop over each character for char in text { … } Each char is a Character

9. Small Practice – Try these now

  1. Create a welcome message with name, age, city using interpolation
  2. Write a function that checks if a string contains both “@” and “.” (basic email check)
  3. Format a 10-digit Indian phone number like: +91 XXXXX XXXXX

Paste your code if you want feedback!

What would you like to explore more deeply next?

  • Advanced string manipulation (regular expressions, splitting, joining, prefix/suffix)
  • Strings + optionals (very common in real apps)
  • Strings in SwiftUI (Text, formatting, markdown)
  • Performance & mutability (String vs NSMutableString vs Substring)
  • Unicode, emoji, flags, skin tones in detail
  • Or move to another topic (arrays, dictionaries, optionals, structs…)

Just tell me — we’ll continue in the same detailed, patient, teacher-like style 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *