迅速从阿拉伯语中删除变音符号

时间:2018-06-04 11:22:05

标签: swift string arabic diacritics arabic-support

我正在尝试删除阿拉伯语文字变音符号。例如,我需要将此َب转换为此ب,这是我的代码:

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")
}

我尝试过这些解决方案,但没有运气:

How to remove diacritics from a String in Swift?

NSString : easy way to remove UTF-8 accents from a string?

4 个答案:

答案 0 :(得分:1)

您可以将CFStringTransformkCFStringTransformStripCombiningMarks

一起使用

删除(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
    }
}

enter image description here

答案 3 :(得分:0)

order