using System;
using System.Text;
namespace Adhar_no
{
class Program
{
public static string MaskAadharNumber(string aadharNumber)
{
if (aadharNumber.Length != 12)
{
throw new ArgumentException("Aadhar number must be exactly 12 digits long.");
}
// Mask the Aadhar number
StringBuilder masked = new StringBuilder();
for (int i = 0; i < 12; i++)
{
// Insert a space every 4 digits, except at the end
if (i > 0 && i % 4 == 0)
{
masked.Append(" ");
}
masked.Append(aadharNumber[i]);
}
return masked.ToString();
}
static void Main(string[] args)
{
try
{
Console.WriteLine("Enter Aadhar Number Here.");
string aadharNumber = Console.ReadLine();
string maskedAadhar = MaskAadharNumber(aadharNumber);
Console.WriteLine("Masked Aadhar Number: " + maskedAadhar);
}
catch (ArgumentException ex)
{
Console.WriteLine("Error: " + ex.Message);
}
Console.Read();
}
}
}
Here we use
- Argument Validation: Check if the Aadhar number provided is exactly 12 digits long. If not, it throws an argument exception.
- StringBuilder Initialization: Initializes a StringBuilder object named masked to build the masked Aadhar number.
- Loop through Aadhar Number: Iterates through each character of the Aadhar number string.
- Insert Spaces: Inserts a space (" ") every 4 characters to create blocks of 4 digits.
- Return Masked Aadhar Number: Returns the final masked Aadhar number as a string.
Final Output
Mask an Account no in c#
In C#, you can use a masked account number.
1. _Substring_
Extract the last few digits of the account number and mask the rest.
string accountNumber = "1234567890";
string maskedNumber = accountNumber.Substring(0, accountNumber.Length - 4) + "****";
Console.WriteLine(maskedNumber); // Output: 123456****
2. _Regex_
Use regular expressions to replace all but the last few digits with a mask.
string accountNumber = "1234567890";
string maskedNumber = System.Text.RegularExpressions.Regex.Replace(
accountNumber,
@"^(\d{6})(\d{4})$",
"$1****"
);
Console.WriteLine(maskedNumber); // Output: 123456****
3. _String.Format_
Use string formatting to display the account number with a mask.
string accountNumber = "1234567890";
string maskedNumber = string.Format("{0:####-****}", Convert.ToInt64(accountNumber));
Console.WriteLine(maskedNumber); // Output: 1234-****
4. Masked an Adhar Number Last Four Digit
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace maskadharcard
{
class Program
{
static void Main(string[] args)
{
string cardNumber = "345612343456";
var cardNumber1 = string.Format("########{0}", cardNumber.Substring(8, 4));
var maskedCardNumberWithSpaces = Regex.Replace(cardNumber1, ".{4}", "$0 ");
Console.WriteLine(maskedCardNumberWithSpaces);
Console.Read();
}
}
}
Output. Finally, a screen is shown.