Java String Handling

Mask part of string example in Java

Mask part of string example shows how to mask part of the String with * asterisk symbol. Example also shows how to hide credit card number or email address with * using various approaches.

How to mask part of string in Java?

Many a times we need to mask part of the string with * character before displaying it on the front-end. For example, displaying a credit card number (where only last four digits are visible while other digits are displayed as an asterisk * character) or displaying an email address where only first character and domain is displayed while rest of the characters are displayed as * symbol.

How to mask credit card numbers with * ?

For masking the credit card number, we are going to assume that the card number is 16 digits long.

1) Mask credit card string using substring method

We are going to write a generic method which we can use to mask the character range from the string. We will use substring method String class to do the same as given below.


The maskString method takes input string, start index, end index and mask character as arguments. If start index is less than 0, we make it 0. If end index is greater than string’s length we assign string’s length to it. Only important condition here is that start index should not be greater than end index.

Next, we determine how many characters we need to mask by taking difference of start and end index. We make string of that length by appending mask character to StringBuilder. For example, if the start index is 0 and end index is 4, mask string will be “****”.

Once we have built the mask string, we take substring from original string starting from 0 index to start index. We concatenate it with the mask string and then again concatenate both of them to the substring starting from index start index + mask length till string’s length. Basically we are replacing the range of characters with the mask string.

2) Mask credit card string using Apache Commons

If you are using Apache commons library, you can use repeat method to create masking string of specified length.

This method returns a string containing specified character repeated n times.

Once we have masking string, we can use overlay method of StringUtils class to overlay part of the String.

This method overlays part of the string with another string.


How to mask part of email address in Java?

Masking credit card number was a bit easy since we assumed that they are 16 characters long. However, in case of an email address we won’t know how many characters are there before @domain part. We are going split the email id in two parts by @ symbol, mask the first part, and then combine it with the domain part. We are also going to reuse maskString method we have written above.


You can change the part you want to mask by changing the start and end index in above program.

Please let us know your views in the comments section below.


Join 1000+ fellow learners! Enter your email address below: