Swift-银行卡格式转换

Swift 项目开发中遇到将银行卡按照4位一组,中间空格的形式展示,扩展了String类,扩展方法如下:

    public func formateForBankCard(joined: String = " ") -> String {
        guard self.characters.count > 0 else {
            return self
        }
        let length: Int = self.characters.count
        let count: Int = length / 4
        var data: [String] = []
        for i in 0.. 0 {
            data.append(self[4 * count..

测试代码:

        print("输出结果:\("123456789".formateForBankCard())")

你可能感兴趣的:(Swift-银行卡格式转换)