Same String, Different Diacritics
Published on Dec 7, 2019Same String, Different Diacritics โ SwiftMoji Entry #7
When searching a database for text entry, it can often be useful to compare results with a predicate without taking into account any differences in capitalization or accents. The Foundation framework has a convenient method called folding(options:locale:) for standardizing Strings for comparison. It is an instance method on String and can be called with dot syntax. The first parameter labeled options takes in an Array of Strings.CompareOptions. CompareOptions are values representing how search algorithms should compare Strings. For example, diacriticInsensitive disregards accents and caseInsensitive omits the need for matching capitalization. The locale parameter simply provides the linguistic context for removing character distinctions. The method is called folding because it performs what is often referred to as character folding. Character folding is the process of transforming a character into another one of its representations. In the code example, the โรกโ in ๐จโ๐พโs name is folded to โaโ and the โSโ in ๐จโ๐ปโs name is folded to โsโ. Lastly, if you wish to explore the capabilities of folding further, the documentation can be found under the Objective-C version on NSString. The folding method was ported over to StringProtocol, but all the corresponding information regarding its usage remains absent.
import Foundation
struct ๐ค {
var name: String
var comparisonName: String {
name.folding(options: [.diacriticInsensitive, .widthInsensitive, .caseInsensitive],
locale: .current)
}
}
var ๐จโ๐ป = ๐ค(name: "Sean")
var ๐จโ๐พ = ๐ค(name: "seรกn")
if ๐จโ๐ป.comparisonName == ๐จโ๐พ.comparisonName {
print("๐จโ๐ป and ๐จโ๐พ have the same name.") // โ
}
Tagged with: