php - Swift,stringByReplacingOccurencesOfString 和替换数组

标签 php arrays dictionary replace swift

我需要替换数字字符。我的意思是我想用字符串中的孟加拉语聊天替换英语字符。比方说,我有两个数组,

var enDigits = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"] var bnDigits = ["১", "২", "৩", "৪", "৫", "৬", "৭", "৮", "৯", "০"]

我是 iOS 新手,绝对是 swift。在 PHP 中,str_replace() 可以轻松做到这一点。但是,我搜索并找到了用于 swift 的 stringByReplacingOccurencesOfString 但无法弄清楚它是否可以与数组一起使用。我需要你的建议。

最佳答案

这是一个非常具体的答案,但如果您只是想用字符替换字符,您可以在源字符串上使用 map 函数,如下所示:

let enDigits: [Character] = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]
let bnDigits: [Character] = ["১", "২", "৩", "৪", "৫", "৬", "৭", "৮", "৯", "০"]

let enString = "Jenny's number is 867-5309"
let bnString = String(map(enString, { ch in
    if let index = find(enDigits, ch) {
        return bnDigits[index]
    }
    return ch
}))
// Jenny's number is ৮৬৭-৫৩০৯

关于php - Swift,stringByReplacingOccurencesOfString 和替换数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26407293/

相关文章:

ios - withContentsofURL 可以声明超时限制吗?

Python 列表到字典

dictionary - 构造深层结构不起作用

python多处理同步更新字典

javascript - Ajax方法submit()不起作用

php - 如何构建在线 Objective-C 编译器

php - array_push 页面刷新后未添加第二项

php - Symfony 表单需要 true

java - 二维数组的 CopyOfRange

C Char多维数组定义