Facundo Ch.
Facundo Ch.

Reputation: 12214

How to apply a mask to a String?

Hi I have a credit card number as a String. I need to apply a mask to hide the CC number:

I have "123-123-123" and I need to get something like "123-XXX-123"

Is there any elegant way to do this? I'm trying to avoid using severals substring() functions...

Thanks in advance

Upvotes: 1

Views: 5668

Answers (2)

jtahlborn
jtahlborn

Reputation: 53694

myCCStr = myCCStr.replaceFirst("-[0-9]{3}-", "-XXX-");

Upvotes: 3

sajoshi
sajoshi

Reputation: 2763

I believe you can achieve this using PADLeft and PADRight functions of String class.

Upvotes: 0

Related Questions