Skip to content

Instantly share code, notes, and snippets.

@jonahaung
Created April 18, 2024 06:11
Show Gist options
  • Save jonahaung/36ee83e6865ed0c7a2e3fcab617825e2 to your computer and use it in GitHub Desktop.
Save jonahaung/36ee83e6865ed0c7a2e3fcab617825e2 to your computer and use it in GitHub Desktop.
Credit cards number formatting
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