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 C#

3 Answers

0 votes
using System;

public class Program
{
    public static string MaskNumber(string input, char maskChar = '*')
    {
        if (string.IsNullOrEmpty(input) || input.Length <= 4)
            return input;

        // Take last 4 characters and pad left with maskChar up to full length
        return input.Substring(input.Length - 4).PadLeft(input.Length, maskChar);

        // Alternatively, using modern C# range syntax:
        // return input[^4..].PadLeft(input.Length, maskChar);
    }

    public static void Main()
    {
        Console.WriteLine(MaskNumber("1234567890123456")); // ************3456
        Console.WriteLine(MaskNumber("9876543210"));       // ******3210
        Console.WriteLine(MaskNumber("123"));              // 123
    }
}



/*
run:

************3456
******3210
123

*/

 



answered 4 hours ago by avibootz
0 votes
using System;

public class Program
{
    public static string MaskNumberExplicit(string input, char maskChar = '*')
    {
        if (string.IsNullOrEmpty(input) || input.Length <= 4)
            return input;
    
        int maskLength = input.Length - 4;
        
        return new string(maskChar, maskLength) + input.Substring(maskLength);
    }

    public static void Main()
    {
        Console.WriteLine(MaskNumberExplicit("1234567890123456")); // ************3456
    }
}



/*
run:

************3456

*/

 



answered 4 hours ago by avibootz
0 votes
using System;
using System.Text.RegularExpressions;

public class Program
{
    public static string MaskDigitsKeepFormat(string input, int visibleDigits = 4, 
                                              char maskChar = '*')
    {
        if (string.IsNullOrEmpty(input))
            return input;

        // Count total digits in the string
        int totalDigits = Regex.Matches(input, @"\d").Count;
        int digitsToMask = Math.Max(0, totalDigits - visibleDigits);
        int maskedCount = 0;

        // Replace digits left-to-right until digitsToMask threshold is reached
        return Regex.Replace(input, @"\d", match =>
        {
            if (maskedCount < digitsToMask)
            {
                maskedCount++;
                return maskChar.ToString();
            }
            return match.Value;
        });
    }

    public static void Main()
    {
        Console.WriteLine(MaskDigitsKeepFormat("1234-5678-9012-3456")); // ****-****-****-3456
        Console.WriteLine(MaskDigitsKeepFormat("+1 (800) 555-0199"));    // +* (***) ***-0199
    }
}



/*
run:

****-****-****-3456
+* (***) ***-0199

*/

 



answered 4 hours ago by avibootz

Related questions

...