Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to convert special characters to normal characters in Swift? [duplicate]

Suppose I have the String value with special accents :

éáüîāṁṇār̥

Is there a universal/dynamic way to convert/strip these types of characters down to a basic formatting like ?..

eauiamnar
like image 386
Trip Avatar asked Apr 19 '16 19:04

Trip


People also ask

How do I remove special characters from a string in Swift?

To remove specific set of characters from a String in Swift, take these characters to be removed in a set, and call the removeAll(where:) method on this string str , with the predicate that if the specific characters contain this character in the String.

How do I convert a string to a character in Swift?

this should be as simple as let characterFromString = Character(textField. text) . NSString is automatically bridged to Swift's String , and the Character class has an init which accepts a single character String ; see the documentation here.

How do you remove the last two characters in Swift?

To remove last character of a String in Swift, call removeLast() method on this string. String. removeLast() method removes the last character from the String.


1 Answers

Use stringByFoldingWithOptions and pass .DiacriticInsensitiveSearch

let s = "éáüîāṁṇār̥"
let r = s.stringByFoldingWithOptions(.DiacriticInsensitiveSearch, locale: nil)
print(r) // prints eauiamnar
like image 199
Sergey Kalinichenko Avatar answered Oct 31 '22 11:10

Sergey Kalinichenko