How to check if a string contains another in Swift
In this tutorial, we are going to learn about how to check if a string contains another string in Swift.
Checking the string contains another string
To check if a string contains another string, we can use the built-in contains()
method in Swift.
The contains()
method takes the string as an argument and returns true
if a substring is found in the string; otherwise it returns false
.
Here is an example:
let name = "alexa hut"
let result = name.contains("hut")
print(result)
Output:
true
False case:
let name = "alexa hut"
let result = name.contains("rome")
print(result)
Output:
false