Skip to content

Latest commit

 

History

History
25 lines (21 loc) · 807 Bytes

mask-text.md

File metadata and controls

25 lines (21 loc) · 807 Bytes
title description author tags
Mask Text
Masks portions of a string, leaving specific parts at the beginning and end visible while replacing the rest with a specified character
Mcbencrafter
string,mask,hide
public static String partialMask(String text, int maskLengthStart, int maskLengthEnd, char mask) 
    if (text == null)
        return null;
    
    StringBuilder maskedText = new StringBuilder();
    maskedText.append(text, 0, maskLengthStart);
    
    for (int currentChar = maskLengthStart; currentChar < text.length(); currentChar++) {
        maskedText.append(mask);
    }
    maskedText.append(text, text.length() - maskLengthEnd, text.length());
    return maskedText.toString();
}

// Usage:
System.out.println(partialMask("1234567890", 4, 2, '*')); // "1234****90"