Created
April 18, 2024 06:11
-
-
Save jonahaung/36ee83e6865ed0c7a2e3fcab617825e2 to your computer and use it in GitHub Desktop.
Credit cards number formatting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension String { | |
func insertStringAt(indexes: [Int], string: String) -> String { | |
var formatted = String() | |
for (i, str) in self.enumerated() { | |
if indexes.contains(i) { | |
formatted.append("\(str)\(string)") | |
} else { | |
formatted.append(str) | |
} | |
} | |
return formatted | |
} | |
} | |
/* | |
Eg: "0000000000000000".insertStringAt(indexes: [3, 7, 11], string: " ") | |
output: "0000 0000 0000 0000" | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment