Chapters

Hide chapters

Swift Cookbook

Live Edition · Multiplatform · Swift · Editor agnostic

Use String Comparison in Swift
Written by Team Kodeco

String comparison allows you to compare two strings to see if they are equal or not.

Here’s how to use string comparison in Swift:

let string1 = "Hello, World!"
let string2 = "Hello, World!"
let string3 = "Goodbye, World!"

print(string1 == string2) // Output: true
print(string1 == string3) // Output: false

You can also use the != operator to check if two strings are not equal:

print(string1 != string3) // Output: true

You can also use the >, <, >=, and <= operators to compare the lexicographic order of two strings:

let string4 = "Apple"
let string5 = "Banana"

print(string4 > string5) // Output: false
print(string4 < string5) // Output: true
© 2024 Kodeco Inc.