March 18, 2020

Check if string contains substring with Swift

Check if string contains substring with Swift

To check if a string contains a substring, you can use the contains method:

let stringValue = "this value"

if stringValue.contains("value") {
    print("yes")
} else {
    print("no")
}

You can also use the contains method to see whether a string array contains a string:

let stringArray = ["this", "value"]

if stringArray.contains("this") {
    print("yes")
} else {
    print("no")
}