Swift Language Strings and Characters Examine and compare strings

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

Check whether a string is empty:

if str.isEmpty {
    // do something if the string is empty
}

// If the string is empty, replace it with a fallback:
let result = str.isEmpty ? "fallback string" : str

Check whether two strings are equal (in the sense of Unicode canonical equivalence):

"abc" == "def"          // false
"abc" == "ABC"          // false
"abc" == "abc"          // true

// "LATIN SMALL LETTER A WITH ACUTE" == "LATIN SMALL LETTER A" + "COMBINING ACUTE ACCENT"
"\u{e1}" == "a\u{301}"  // true

Check whether a string starts/ends with another string:

"fortitude".hasPrefix("fort")      // true
"Swift Language".hasSuffix("age")  // true


Got any Swift Language Question?