SoFunction
Updated on 2025-05-07

Write a character desensitization tool class in Java

1. Character desensitization tools

import .slf4j.Slf4j;
import .;

/**
  * Data desensitization tools
  *
  * @date 2024/10/30 13:44
  */

@Slf4j
public class DataDesensitizationUtils {

    public static final String STAR_1 = "*";
    public static final String STAR_2 = "**";
    public static final String STAR_3 = "***";
    public static final String STAR_4 = "****";

    /**
      * Mobile phone number desensitization
      *
      * @param mobile phone number
      * @return Return value
      */
    public static String mobileDesensitize(String mobile) {
        // If the phone number is empty, return an empty string        if ((mobile)) {
            return "";
        }

        // Get the last 4 digits of the mobile phone number        String right = (mobile, 4);
        // All mobile phone numbers are replaced with * except for the last 4 digits.        String leftPad = (right, (mobile), STAR_1);
        // Remove the previous default three-digit*        String removeStart = (leftPad, STAR_3);
        // Get the first 3 digits of the mobile phone number        String left = (mobile, 3);
        // String link and return        return (removeStart);
    }

    /**
      * Name desensitized
      *
      * @param name
      * @return Return value
      */
    public static String nameDesensitize(String name) {
        // If the name is empty, the empty string will be returned        if ((name)) {
            return "";
        }

        // If the length of the name is less than or equal to 3 characters, the previous character will be replaced by *        int length = 3;
        if (() <= length) {
            int len = () - 1;
            String right = (name, len);
            return STAR_1.concat(right);
        }

        // If the name length is greater than 3 characters, the first two characters will be replaced by * if it is replaced by * if it is longer than 3 characters.        int len = () - 2;
        String right = (name, len);
        return STAR_2.concat(right);
    }

    /**
      * Address desensitization
      *
      * @param detailAddress Address
      * @return Return value
      */
    public static String detailAddressDesensitize(String detailAddress) {
        int length = 10;
        String desensitizationText = STAR_4;
        // If the address is empty or the length is less than 10 characters, the default return is *        if ((detailAddress) || () <= length) {
            return desensitizationText;
        } else {
            // Replace all characters after the address 10 characters with *            StringBuilder address = new StringBuilder(detailAddress);
            int start = () - 10;
            int end = ();
            return (start, end, desensitizationText).toString();
        }
    }

	/**
      * Desensitizes part of the content of the specified string and replaces it with the specified number of asterisks.
      *
      * @param start The starting position where desensitization is required (included)
      * @param end The end position that requires desensitization (not included)
      * @param content Original string
      * @param maxStarSize Maximum number of asterisks, the excess will be truncated
      * @return Processed string
      */
    public static String contentDesensitize(int start, int end, String content, int maxStarSize) {
        // If the string is empty, the empty string will be returned        if ((content)) {
            return "";
        }

        // The starting position cannot be less than 0        start = (0, start);
        // The end position cannot be greater than the maximum length of the string        end = ((), end);

        // If the end position is smaller than the start position or the end position is larger than the original string length, return to the original string.        if (end < start) {
            return content;
        }

        // Take the minimum value of both, that is, maxStarSize at most * numbers appear in the middle        int min = (end - start, maxStarSize);
        // Take the maximum value of the two as the number of repetitions, that is, ensure that the number of repetitions of number * is not negative        int count = (0, min);
        // The * string that calculates the count times, repeat is a method provided by JDK11+        // JDK8 can use the following methods        // String repeat = (STAR_1, count);
        String repeat = STAR_1.repeat(count);
        // Replace the plaintext between the string start and end with *        return new StringBuilder(content)
                .replace(start, end, repeat)
                .toString();
    }

    /**
      * Desensitizes part of the content of the specified string and replaces it with the specified number of asterisks.
      *
      * @param start The starting position where desensitization is required (included)
      * @param end The end position that requires desensitization (not included)
      * @param content Original string
      * @return Processed string
      */
    public static String contentDesensitize(int start, int end, String content) {
        // The default maximum number of * numbers is the number of intermediate replacement characters        return contentDesensitize(start, end, content, end - start);
    }

}

2. Test tool category

@Slf4j
@ExtendWith()
public class DataDesensitizationUtilsTest {

    @Test
    public void testMobileDesensitize() {
        String mobile = "13011112222";
        String result = (mobile);
        ("Mobile phone number desensitization results:{}", result);
    }

    @Test
    public void testNameDesensitize() {
        String name = "Zhuge Kongming";
        String result = (name);
        ("Name desensitization results:{}", result);
    }

    @Test
    public void testAddressDesensitize() {
        String address = "Beijing Shougang International Convention and Exhibition Center Hall 9 and Hall 10";
        String result = (address);
        ("Address desensitization results:{}", result);
    }

	@Test
    public void testStringUtils() {
        String param = "Beijing Shougang International Convention and Exhibition Center Hall 9 and Hall 10";
        String result1 = (param, 3);
        ("Processing results1:{}", result1);

        String result2 = (param, 4);
        ("Processing results2:{}", result2);

        String result3 = (result2, (), "*");
        ("Processing results3:{}", result3);
    }
    
	@Test
    public void testContentDesensitize() {
        String param = "Zhuge Kongming";
        String result1 = (1, 7, param);
        ("Processing results1:{}", result1);
        String result2 = (1, () - 7, param, 6);
        ("Processing results2:{}", result2);
        String result3 = (0, () + 7, param, 6);
        ("Processing results3:{}", result3);
    }

}

3. Test results

2024-10-31 09:36:11 [INFO ] [main] - [] - ():30 - Name desensitization result: **Kong Ming
2024-10-31 09:36:11 [INFO ] [main] - [] - ():23 - Mobile phone number desensitization result: 130****2222
2024-10-31 09:36:11 [INFO ] [main] - [] - ():37 - Address desensitization results: Beijing Shougang International Exhibition****
2024-10-31 09:36:11 [INFO ] [main] - [] - ():44 - Processing result 1: Beijing first
2024-10-31 09:36:11 [INFO ] [main] - [] - ():47 - Processing result 2: Hall 10
2024-10-31 09:36:11 [INFO ] [main] - [] - ():50 - Processing result 3: Hall No. 10, ******************
2024-11-01 09:39:37 [INFO ] [main] - [] - ():74 - Processing result 1: ***
2024-11-01 09:39:37 [INFO ] [main] - [] - ():76 - Processing result 2: Zhuge Kongming
2024-11-01 09:39:37 [INFO ] [main] - [] - ():78 - Processing result 3: ****

This is the end of this article about writing a character desensitization tool using Java. For more related Java character desensitization content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!