gpt4 book ai didi

ios - 如何从 ios 中的字符串中查找重音字符?

转载 作者:行者123 更新时间:2023-11-28 06:00:48 25 4
gpt4 key购买 nike

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

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

var str = "Hello, plaAyground"
var scalars = str.unicodeScalars

str = "Hello, plåÅyground"
scalars = str.unicodeScalars
print(scalars.dropFirst())

最佳答案

您可以创建一个自定义的 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
}

关于ios - 如何从 ios 中的字符串中查找重音字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49817356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com