📖 Swift String Methods – Full Guide
1. Initialization
let emptyString = ""
let anotherEmptyString = String()
let fromCharacterArray = String(["H", "e", "l", "l", "o"])
2. Properties
let str = "Hello, Swift!"
print(str.isEmpty)
print(str.count)
print(str.startIndex)
print(str.endIndex)
3. Accessing Characters
for char in str {
print(char)
}
print(str[str.startIndex])
print(str[str.index(before: str.endIndex)])
4. Modifying Strings
Inserting
var greeting = "Hello"
greeting.insert("!", at: greeting.endIndex)
Removing
greeting.remove(at: greeting.index(before: greeting.endIndex))
greeting.removeSubrange(greeting.index(greeting.endIndex, offsetBy: -2)..<greeting.endIndex)
5. Concatenation
let first = "Hello"
let second = " World"
let result = first + second
var greetingAgain = "Hi"
greetingAgain += " there"
6. String Interpolation
let name = "John"
let age = 30
let intro = "My name is \(name) and I am \(age) years old."
7. Prefix and Suffix Checks
let filename = "photo.jpg"
print(filename.hasPrefix("photo"))
print(filename.hasSuffix(".jpg"))
8. Changing Case
let text = "Swift Programming"
print(text.uppercased())
print(text.lowercased())
print(text.capitalized)
9. Finding Substrings
let message = "Hello, world!"
if let range = message.range(of: "world") {
print(message[range])
}
10. Replacing Substrings
var welcome = "Hello, playground"
welcome = welcome.replacingOccurrences(of: "playground", with: "world")
11. Splitting Strings
let sentence = "apple,banana,cherry"
let fruits = sentence.split(separator: ",")
for fruit in fruits {
print(fruit)
}
12. Joining Strings
let words = ["Swift", "is", "awesome"]
let joined = words.joined(separator: " ")
13. Trimming Whitespaces and Newlines
let messy = " Hello World! \n"
let cleaned = messy.trimmingCharacters(in: .whitespacesAndNewlines)
14. Substrings and Slicing
let str2 = "Hello, Swift!"
let index = str2.firstIndex(of: ",") ?? str2.endIndex
let substring = str2[..<index]
let newString = String(substring)
15. Contains Check
let phrase = "I love Swift programming."
print(phrase.contains("Swift"))
16. Comparing Strings
let a = "apple"
let b = "Apple"
print(a == b)
print(a.lowercased() == b.lowercased())
17. Advanced Methods
dropFirst() and dropLast()
let greet = "Hello"
let dropped = greet.dropFirst()
prefix(_:) and suffix(_:)
let text2 = "abcdef"
print(text2.prefix(3))
print(text2.suffix(2))
split(whereSeparator:)
let complex = "Swift;Kotlin;Java"
let parts = complex.split { $0 == ";" }
18. Localization (Case Insensitive)
let city = "München"
print(city.lowercased())
print(city.folding(options: .diacriticInsensitive, locale: .current))
🌟 Quick Summary Table
Feature |
Method/Property |
Example |
Empty check | .isEmpty | "abc".isEmpty |
Length | .count | "abc".count |
Uppercase | .uppercased() | "abc".uppercased() |
Lowercase | .lowercased() | "ABC".lowercased() |
Replace substring | .replacingOccurrences | "abc".replacingOccurrences(of:"a", with:"b") |
Trim whitespace | .trimmingCharacters | " abc ".trimmingCharacters(in: .whitespaces) |
Split string | .split(separator:) | "a,b,c".split(separator:",") |
Join strings | .joined(separator:) | ["a","b"].joined(separator:",") |
Contains | .contains | "abc".contains("a") |