String Manipulation in Swift

Perform common string operations in Swift, such as checking for substrings, splitting strings, and converting case.

String Manipulation in Swift
Photo by Dmitry Chernyshov / Unsplash

Perform common string operations in Swift, such as checking for substrings, splitting strings, and converting case.

// Check if a string contains another string
let mainString = "Hello, World!"
let substring = "Hello"
if mainString.contains(substring) {
    print("Main string contains the substring")
}

// Split a string into an array of substrings
let sentence = "This is a sentence"
let words = sentence.split(separator: " ")
print(words) // ["This", "is", "a", "sentence"]

// Convert a string to lowercase
let uppercaseString = "HELLO"
let lowercaseString = uppercaseString.lowercased()
print(lowercaseString) // "hello"