Created
November 3, 2022 13:27
-
-
Save G00fY2/c6d6c2a7a0983a32e33646fe8d06d92a to your computer and use it in GitHub Desktop.
Extension function to format IBAN Strings
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
fun String.formatIBAN(): String { | |
val iban = replace("\\s+".toRegex(), "").uppercase(Locale.getDefault()) | |
val maxLength = (iban.length * 1.25).roundToInt() | |
val result = StringBuilder(maxLength) | |
iban.forEachIndexed { index, c -> | |
when { | |
index > 0 && index % 4 == 0 -> result.append(" $c") | |
else -> result.append(c) | |
} | |
} | |
return result.toString() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment