Given a string that represents a credit card number...
val creditCardNo = "1111222233334444"
... how do I mask the first 12 characters with *
?
val maskedCreditCardNo = "************4444"
Just use drop
or substring
on the original number, and prepend the right number of "*":
"*" * 12 + (creditCardNo drop 12)
Replace all digit symbols unless 4 characters remain:
creditCardNo.replaceAll("\\d(?=\\d{4})", "*")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With