Swift String elementsEqual()

The elementsEqual() method returns true if two strings are equal. If not, it returns false.

Example


elementsEqual() Syntax

The syntax of the string elementsEqual() method is:

string.elementsEqual(str: String)

Here, string is an object of the String class.


elementsEqual() Parameters

The Equals() method takes a single parameter:

  • str - the string to be compared with the given string

elementsEqual() Return Value

The elementsEqual() method returns:

  • true - if the strings are equal
  • false - if the strings are not equal

Example 1: Swift String elementsEqual()

Here,

  • str1 and str2 are equal. Hence, str1.elementsEqual(str2) returns true.
  • str1 and str3 are not equal. Hence, str1.elementsEqual(str3) and str3.elementsEqual(str1) return false.

Example 2: Check if Two Strings Are Equal

Output

str1 and str2 are not equal

Example 3: elementsEqual() With Case

When "Learn Swift" is compared to "learn Swift", we get false. This is because elementsEqual() takes the letter case into consideration.