Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove diacritics from a String in Swift?

Tags:

string

swift

How to remove diacritics (or accents) from a String (like say change "één" to "een") in Swift? Do I have to go back to NSString or can it be done within Swift?

like image 619
Johan Kool Avatar asked Apr 08 '15 17:04

Johan Kool


1 Answers

You can operate directly on a Swift String (if "Foundation" is imported):

let foo = "één" let bar = foo.stringByFoldingWithOptions(.DiacriticInsensitiveSearch, locale: NSLocale.currentLocale()) print(bar) // een 

Swift 3:

let foo = "één" let bar = foo.folding(options: .diacriticInsensitive, locale: .current) print(bar) // een 
like image 83
Martin R Avatar answered Sep 29 '22 18:09

Martin R