原来以为简体与繁体转换挺难实现的,后来看了GBig代码后才恍然大悟。其实非常简单就是对表查询。准备一个简体字符串,准备一个繁体字符串。
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print(GBig.gb.utf8.count)
print("東加拿大".gb)
print("东加拿大".big5)
// Do any additional setup after loading the view.
}
}
程序源代码
public static func simplify(_ ch: String) -> String {
return String(ch.map { c in
if let i = big5.index(of: c) {
return gb[i]
} else {
return c
}
})
}
public static func traditionalize(_ ch: String) -> String {
return String(ch.map { c in
if let i = gb.index(of: c) {
return big5[i]
} else {
return c
}
})
}
}
public extension String {
public var big5: String {
return GBig.traditionalize(self)
}
public var gb: String {
return GBig.simplify(self)
}
}
代码下载
https://github.com/zhishidapang/GBig
网友评论