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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
package com.javacodeexamples.stringexamples; public class MaskStringExample { public static void main(String[] args) throws Exception { String str = "1234567812345678"; //mask first 4 characters System.out.println( maskString(str, 0, 4, '*') ); //mask everything but last 4 digits System.out.println( maskString(str, 0, 12, '*') ); //mask everything System.out.println( maskString(str, 0, str.length(), '*') ); //mask everything but first and last 4 digits System.out.println( maskString(str, 1, 12, '*') ); } private static String maskString(String strText, int start, int end, char maskChar) throws Exception{ if(strText == null || strText.equals("")) return ""; if(start < 0) start = 0; if( end > strText.length() ) end = strText.length(); if(start > end) throw new Exception("End index cannot be greater than start index"); int maskLength = end - start; if(maskLength == 0) return strText; StringBuilder sbMaskString = new StringBuilder(maskLength); for(int i = 0; i < maskLength; i++){ sbMaskString.append(maskChar); } return strText.substring(0, start) + sbMaskString.toString() + strText.substring(start + maskLength); } } |
Output
1 2 3 4 |
****567812345678 ************5678 **************** 1***********5678 |
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.
1 |
public static String repeat(char ch, int n) |
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.
1 |
public static String overlay(String sourceString, String overlayString, int start, int end) |
This method overlays part of the string with another string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
package com.javacodeexamples.stringexamples; import org.apache.commons.lang3.StringUtils; public class MaskStringExample { public static void main(String[] args) throws Exception { String str = "1234567812345678"; //mask first 4 characters System.out.println( maskString(str, 0, 4, '*') ); //mask everything but last 4 digits System.out.println( maskString(str, 0, 12, '*') ); //mask everything System.out.println( maskString(str, 0, str.length(), '*') ); //mask everything but first and last 4 digits System.out.println( maskString(str, 1, 12, '*') ); } private static String maskString(String strText, int start, int end, char maskChar) throws Exception{ if(strText == null || strText.equals("")) return ""; if(start < 0) start = 0; if( end > strText.length() ) end = strText.length(); if(start > end) throw new Exception("End index cannot be greater than start index"); int maskLength = end - start; if(maskLength == 0) return strText; String strMaskString = StringUtils.repeat(maskChar, maskLength); return StringUtils.overlay(strText, strMaskString, start, end); } } |
Output
1 2 3 4 |
****567812345678 ************5678 **************** 1***********5678 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
package com.javacodeexamples.stringexamples; import org.apache.commons.lang3.StringUtils; public class MaskStringExample { public static void main(String[] args) throws Exception { String[] emailIds = { }; for(String emailId : emailIds) System.out.println(maskEmailAddress(emailId, '*')); } private static String maskEmailAddress(String strEmail, char maskChar) throws Exception{ String[] parts = strEmail.split("@"); //mask first part String strId = ""; if(parts[0].length() < 4) strId = maskString(parts[0], 0, parts[0].length(), '*'); else strId = maskString(parts[0], 1, parts[0].length()-1, '*'); //now append the domain part to the masked id part return strId + "@" + parts[1]; } private static String maskString(String strText, int start, int end, char maskChar) throws Exception{ if(strText == null || strText.equals("")) return ""; if(start < 0) start = 0; if( end > strText.length() ) end = strText.length(); if(start > end) throw new Exception("End index cannot be greater than start index"); int maskLength = end - start; if(maskLength == 0) return strText; StringBuilder sbMaskString = new StringBuilder(maskLength); for(int i = 0; i < maskLength; i++){ sbMaskString.append(maskChar); } return strText.substring(0, start) + sbMaskString.toString() + strText.substring(start + maskLength); } } |
Output
1 2 3 4 |
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.
Thank you very much!
Thanks, glad you liked the example.
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.
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
****************
****************
***************