Skip to content

Instantly share code, notes, and snippets.

@G00fY2
Created November 3, 2022 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save G00fY2/c6d6c2a7a0983a32e33646fe8d06d92a to your computer and use it in GitHub Desktop.
Save G00fY2/c6d6c2a7a0983a32e33646fe8d06d92a to your computer and use it in GitHub Desktop.
Extension function to format IBAN Strings
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