Swift – Check if Strings are Not Equal
To check if Strings are not equal in Swift, we can use not equal to !=
operator and provide the two strings as operands to this not equal to operator.
The syntax to use not equal to operator with the strings is
</>
Copy
str1 != str2
Not Equal to operator returns true
if the strings are not equal, or false
if the strings are equal.
Example
In the following program, we take two strings in variables str1
and str2
, and check if they are not equal.
main.swift
</>
Copy
var str1 = "mango"
var str2 = "apple"
if (str1 != str2) {
print("\(str1) is not equal to \(str2)")
} else {
print("\(str1) is equal to \(str2)")
}
Output
Conclusion
In this Swift Tutorial, we learned how to check if strings are not equal using Not Equal to operator.