Skip to content

Mask part of string example in Java

Mask part of string example shows how to mask part of the String with * asterisk symbol. The 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 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 the last four digits are visible while other digits are displayed as an asterisk * character) or displaying an email address where only the first character and domain is displayed while the 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 the 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 out the String substring method example for more details on how the substring method works.

Output

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

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

Once we have built the mask string, we take a substring from the 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 a 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 the StringUtils class to overlay part of the String.

This method overlays part of the string with another string.

Output

How to mask part of the email address in Java?

Masking credit card numbers 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 the @domain part. We are going split the email id into 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 the split method.

Output

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

This example is a part of Java String tutorial.

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

About the author

3 comments

  1. Hi… I have a requirement where i need to mask a text field value in front end and unmask the same at serverlet… How do i achieve this? Appreciate you help on this.

  2. Hi, I saw your snippets, so my requirement is to mask all the fields after the signature. (Name, Contact, Designation, Email ID etc.). Please can you help me to solve this. Thanks in Advance.

    Example:

    Thanks & Regards
    ****************
    ****************
    ***************

Leave a Reply

Your email address will not be published.