Swift Strings
🧵 Swift Strings
Swift strings are powerful, Unicode-safe, and easy to use.
🔹 1. What is a String?
A String is a collection of characters.
🔹 2. Creating Strings
String Literal
Empty String
🔹 3. Printing Strings
🔹 4. String Interpolation (Very Important 🔥)
Insert values inside strings using \( ).
✔ Safe
✔ Clean
✔ Recommended
🔹 5. Concatenating Strings
Using +
Using +=
🔹 6. Multi-line Strings
Use triple quotes """.
🔹 7. String Length
Use .count.
Output:
🔹 8. Checking Empty String
🔹 9. Accessing Characters
⚠️ Direct indexing like name[0] is not allowed in Swift.
🔹 10. Common String Methods
| Method | Example | Result |
|---|---|---|
| Uppercase | text.uppercased() |
HELLO SWIFT |
| Lowercase | text.lowercased() |
hello swift |
| Contains | text.contains("Swift") |
true |
| Has Prefix | text.hasPrefix("Hello") |
true |
| Has Suffix | text.hasSuffix("Swift") |
true |
🔹 11. Comparing Strings
🔹 12. Convert String to Number
❌ Common Mistakes
❌ Adding string with number:
✅ Correct:
🧠 Summary
-
Strings store text data
-
Use
"for strings -
Use
\( )for interpolation -
.countfor length -
strings are Unicode-safe
