我需要从字符串中找到重音字符,而不是删除字符。
我尝试使用以下代码,但不知道如何识别重音字符?
my.js.coffee
答案 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
}