Mask all digits except first 6 and last 4 digits of a string( length varies) in C#

Mask all digits except first 6 and last 4 digits of a string( length varies) in C#

To mask all digits except the first 6 and last 4 digits of a string, you can use the string.Substring method to extract the first 6 and last 4 characters of the string, and then use a regular expression to replace all digits in the remaining characters with an asterisk.

Here's an example implementation:

public static string MaskString(string input) { if (string.IsNullOrEmpty(input)) { return input; } string firstSixChars = input.Substring(0, Math.Min(input.Length, 6)); string lastFourChars = input.Substring(Math.Max(0, input.Length - 4)); string remainingChars = input.Substring(firstSixChars.Length, input.Length - firstSixChars.Length - lastFourChars.Length); string maskedChars = Regex.Replace(remainingChars, @"\d", "*"); return $"{firstSixChars}{maskedChars}{lastFourChars}"; } 

In this example, the MaskString method takes a string input and returns a masked version of the input string. If the input string is null or empty, the method returns the input string unchanged.

The method then extracts the first 6 and last 4 characters of the input string using the Substring method, and computes the remaining characters by subtracting the lengths of the first 6 and last 4 characters from the total length of the input string.

The remaining characters are then masked by using a regular expression to replace all digits (\d) with an asterisk (*).

Finally, the method returns a concatenated string consisting of the first 6 characters, the masked characters, and the last 4 characters.

Examples

  1. "C# mask all but first 6 and last 4 digits in a string"

    • Description: Learn how to mask a string in C# by replacing all digits except the first 6 and last 4 with asterisks. This example uses string manipulation techniques to achieve the desired result.
    • Code:
      using System; class Program { static string MaskString(string input) { if (input.Length <= 10) return input; // No need to mask if the length is less than or equal to 10 // Extract and mask the middle digits string middleDigits = new string('*', input.Length - 10); // Concatenate the masked result return input.Substring(0, 6) + middleDigits + input.Substring(input.Length - 4); } static void Main() { // Test the masking function string original = "1234567890123456"; string masked = MaskString(original); Console.WriteLine($"Original: {original}"); Console.WriteLine($"Masked: {masked}"); } } 
  2. "C# replace digits with asterisks leaving first 6 and last 4"

    • Description: Understand how to replace all digits in a string with asterisks, excluding the first 6 and last 4 digits. This example showcases the use of regular expressions for efficient pattern matching and replacement.
    • Code:
      using System; using System.Text.RegularExpressions; class Program { static string MaskDigits(string input) { if (input.Length <= 10) return input; // No need to mask if the length is less than or equal to 10 // Use regular expression to replace middle digits with asterisks string pattern = @"(?<=\d{6})\d(?=\d{4})"; string replacement = "*"; string masked = Regex.Replace(input, pattern, replacement); return masked; } static void Main() { // Test the masking function string original = "1234567890123456"; string masked = MaskDigits(original); Console.WriteLine($"Original: {original}"); Console.WriteLine($"Masked: {masked}"); } } 
  3. "C# mask credit card number leaving first 6 and last 4 digits"

    • Description: Explore how to mask a credit card number in C# by keeping the first 6 and last 4 digits visible. This example ensures secure handling of sensitive information in a string.
    • Code:
      using System; class Program { static string MaskCreditCard(string input) { if (input.Length != 16) return input; // Return original if the length is not 16 // Mask the middle digits string middleDigits = new string('*', 6); // Concatenate the masked result return input.Substring(0, 6) + middleDigits + input.Substring(12); } static void Main() { // Test the masking function string original = "1234567890123456"; string masked = MaskCreditCard(original); Console.WriteLine($"Original: {original}"); Console.WriteLine($"Masked: {masked}"); } } 

More Tags

pfx mobile-website gdi uibutton applicationpoolidentity mongodb appsettings astronomy word2vec fire-sharp

More C# Questions

More Bio laboratory Calculators

More Gardening and crops Calculators

More Biochemistry Calculators

More Mixtures and solutions Calculators