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 of the String class to do the same as given below. Check String substring method example for more details on how substring method works.


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. Visit String split example to learn more about split method.


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

Lastly, visit Java String examples page to learn more about String. Please let us know your views in the comments section below.

About the author



rahimv has over 15 years of experience in designing and developing Java applications. His areas of expertise are J2EE and eCommerce. If you like the website, follow him on Facebook, Twitter or Google Plus.


Your email address will not be published. Required fields are marked *