我正在尝试删除阿拉伯语文本变音符号。例如,我需要将此 َ
转换为此 ب
,这是我的代码:
if (text != "") {
for char in text! {
print(char)
print(char.unicodeScalars.first?.value)
if allowed.contains("\(char)"){
newText.append(char)
}
}
self.textView.text = text!
} else {
// TODO :
// show an alert
print("uhhh no way")
}
我已经尝试过这些解决方案但没有成功:
最佳答案
你可以使用正则表达式,试试这段代码
let myString = "الْحَمْدُ لِلَّهِ رَبِّ الْعَالَمِينَ"
let regex = try! NSRegularExpression(pattern: "[\\u064b-\\u064f\\u0650-\\u0652]", options: NSRegularExpression.Options.caseInsensitive)
let range = NSMakeRange(0, myString.unicodeScalars.count)
let modString = regex.stringByReplacingMatches(in: myString, options: [], range: range, withTemplate: "")
print(modString)
Output : الحمد لله رب العالمين
关于 swift 从阿拉伯语中删除变音符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50679443/