How to compare strings without case sensitivity in Swift

Milan Panchal
4 min readFeb 18, 2024

Overview

Swift is a robust programming language that offers many features and functionalities for working with strings.

One of these features is the ability to compare strings without considering their case, or the difference between uppercase and lowercase letters.

This is handy when checking user input, finding specific words, or matching text patterns.

In this article, we will explore how to compare strings in Swift without case sensitivity, using different methods and operators.

Using the == Operator with lowercased() or uppercased()

One of the simplest ways to compare strings in Swift without case sensitivity is to use the == operator with the lowercased() or uppercased() methods. These methods return a new string that is either all lowercase or all uppercase, respectively. By applying these methods to both strings before comparing them, we can ensure that the case does not affect the result.

The lowercased() or uppercased() methods work for most cases, but they have some limitations. For instance, they do not handle some special characters or diacritics correctly, such as ß in German or é in French.

--

--

Milan Panchal

iOS Team Lead ǁ Swift ǁ Objective-C ǁ Lifelong Learner ǁ Blogger — Join Medium from the following link: https://medium.com/@milanpanchal24/membership