在文本Swift,IOS中将英文编号更改为波斯文编号

时间:2017-08-10 07:18:12

标签: ios swift

您好如何在IOS Swift中用字符串中的波斯语替换英文数字

"This is a english text with this numbers 09122223231 34322 "

1 个答案:

答案 0 :(得分:2)

将英文数字转换为波斯语的小函数: -

func convertEngNumToPersianNum(num: String)->String{
    //let number = NSNumber(value: Int(num)!)
    let format = NumberFormatter()
    format.locale = Locale(identifier: "fa_IR")
    let number =   format.number(from: num)
    let faNumber = format.string(from: number!)
    return faNumber!

}
let string = "This is a english text with this numbers 09122223231 34322 "
if let number = Int(string.components(separatedBy: CharacterSet.decimalDigits.inverted).joined()) {
 let perNum = convertEngNumToPersianNum(num: String(number))
    print(perNum)
}

工作证明: - enter image description here