Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,406 questions

56,397 answers

573 users

How to mask a number with stars except the last 4 digits in Java

4 Answers

0 votes
public class Program {
    public static String mask_number_with_stars_except_the_last_4_digits(String cardNumber) {
        String last4Digits = cardNumber.substring(cardNumber.length() - 4);

        String maskedNumber = String.format("%1$" + cardNumber.length() + "s", 
                                            last4Digits).replace(' ', '*');
        
        return maskedNumber;
    }
    
    public static void main(String[] args) {
        String cardNumber = "9003125334656789";

        String maskedNumber = mask_number_with_stars_except_the_last_4_digits(cardNumber);

        System.out.println(maskedNumber);


    }
}

 
/*
run
 
************6789
 
*/

 



answered May 29, 2024 by avibootz
edited 8 hours ago by avibootz
0 votes
public class MaskNumber {
    public static String maskNumber(String input) {
        if (input == null || input.length() <= 4) {
            return input;
        }

        return input.replaceAll(".(?=.{4})", "*");
    }

    public static void main(String[] args) {
        System.out.println(maskNumber("1234567890123456")); // ************3456
        System.out.println(maskNumber("9876543210"));       // ******3210
        System.out.println(maskNumber("123"));              // 123
    }
}

 
/*
run
 
************3456
******3210
123
 
*/

 



answered 8 hours ago by avibootz
0 votes
public class MaskNumberJava11 {
    public static String maskNumber(String input) {
        if (input == null || input.length() <= 4) {
            return input;
        }
        int maskLength = input.length() - 4;
        
        return "*".repeat(maskLength) + input.substring(maskLength);
    }

    public static void main(String[] args) {
        String card = "1234567890123456";
        
        System.out.println(maskNumber(card)); // ************3456
    }
}

 
/*
run
 
************3456
 
*/

 



answered 8 hours ago by avibootz
0 votes
public class MaskDigitsKeepFormat {
    public static String maskDigits(String input, int visibleDigits) {
        if (input == null) return null;

        // Count total digits
        long totalDigits = input.chars().filter(Character::isDigit).count();
        long digitsToMask = Math.max(0, totalDigits - visibleDigits);

        StringBuilder result = new StringBuilder();
        long maskedCount = 0;

        for (char c : input.toCharArray()) {
            if (Character.isDigit(c)) {
                if (maskedCount < digitsToMask) {
                    result.append('*');
                    maskedCount++;
                } else {
                    result.append(c);
                }
            } else {
                result.append(c); // Preserve formatting characters like '-', ' ', etc.
            }
        }

        return result.toString();
    }

    public static void main(String[] args) {
        System.out.println(maskDigits("1234-5678-9012-3456", 4)); // ****-****-****-3456
        System.out.println(maskDigits("+1 (800) 555-0199", 4));    // +* (***) ***-0199
    }
}

 
/*
run
 
****-****-****-3456
+* (***) ***-0199
 
*/

 



answered 7 hours ago by avibootz
...