我正在尝试删除阿拉伯语文字变音符号。例如,我需要将此َب
转换为此ب
,这是我的代码:
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")
}
我尝试过这些解决方案,但没有运气:
答案 0 :(得分:1)
您可以将CFStringTransform
与kCFStringTransformStripCombiningMarks
删除(accents or diacritics)
let original = "ََب"
let mutableString = NSMutableString(string: original) as CFMutableString
CFStringTransform(mutableString, nil, kCFStringTransformStripCombiningMarks, Bool(truncating: 0))
let normalized = (mutableString as NSMutableString).copy() as! NSString
print(normalized)
<强> CFStringTransform 强>
通过删除包含字符串转换的常量 结合标记。
<强> kCFStringTransformStripCombiningMarks 强>
用于剥离组合标记(重音符号或变音符号)的变换的标识符。
答案 1 :(得分:1)
您可以使用Regex,尝试此代码
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)
输出:الحمدللهربالعالمين
答案 2 :(得分:0)
使用此扩展名:
extension String {
/// strip combining marks (accents or diacritics)
var stripDiacritics: String {
let mStringRef = NSMutableString(string: self) as CFMutableString
CFStringTransform(mStringRef, nil, kCFStringTransformStripCombiningMarks, false)
return mStringRef as String
}
}
答案 3 :(得分:0)
order