How to check if string contains substring with Swift

How to check if string contains substring with Swift

Checking if a string contains a substring in Swift is very easy. Strings in Swift have a built in method called contains. This method with allow us to check if a string contains a given substring.

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")
}