如何在ios中找到字符串中的重音字符?

时间:2018-04-13 12:47:11

标签: ios swift

我需要从字符串中找到重音字符,而不是删除字符。

我尝试使用以下代码,但不知道如何识别重音字符?

my.js.coffee

1 个答案:

答案 0 :(得分:1)

您可以创建包含所需字符的自定义CharacterSet,然后在目标字符串中搜索该字符集中出现的字符

let str = "Hello, plåÅyground"
let characters = "åÅ" // put all required characters in that string
let characterSet = CharacterSet(charactersIn: characters)
if let _ = str.rangeOfCharacter(from: characterSet) {
     // do stuff here
}

如果要区分大写/小写的出现次数,请为大写/小写字符创建单独的字符集

let str = "Hello, plåÅyground"
let uppercaseCharacters = "Å"
let lowercaseCharacters = "å"
let uppercaseCharacterSet = CharacterSet(charactersIn: uppercaseCharacters)
let lowercaseCharacterSet = CharacterSet(charactersIn: lowercaseCharacters)
if let _ = str.rangeOfCharacter(from: uppercaseCharacterSet) {
     // uppercase character found
} else if let _ = str.rangeOfCharacter(from: lowercaseCharacterSet) {
    // lowercase character found
}