迅速从阿拉伯语中删除变音符号 [英] swift remove diacritic from Arabic

查看:248
本文介绍了迅速从阿拉伯语中删除变音符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to remove the Arabic text diacritic. For example I need to convert this َب to this ب , here is my code :

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

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

I have tried these solutions but with no luck :

如何在Swift中从字符串中删除变音符号?/a>

How to remove diacritics from a String in Swift?

NSString:删除UTF的简便方法字符串中有-8个重音符号?

推荐答案

您可以使用Regex,尝试使用此代码

You can use Regex, try this code

 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 : الحمد لله رب العالمين

这篇关于迅速从阿拉伯语中删除变音符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆