I have a card number as a string, for example:
string ClsCommon.str_CardNumbe r = \"3456123434561234\";
The length of this card number can
This will work with any card number length:
var cardNumber = "3456123434561234";
var firstDigits = cardNumber.Substring(0, 6);
var lastDigits = cardNumber.Substring(cardNumber.Length - 4, 4);
var requiredMask = new String('X', cardNumber.Length - firstDigits.Length - lastDigits.Length);
var maskedString = string.Concat(firstDigits, requiredMask, lastDigits);
var maskedCardNumberWithSpaces = Regex.Replace(maskedString, ".{4}", "$0 ");
Linq saves coding lines, small code snippet.
Replaces with (*) char above 6 and bellow CardPan length minus 4
var CardPan = "1234567890123456";
var maskedPan = CardPan.Aggregate(string.Empty, (value, next) =>
{
if (value.Length >= 6 && value.Length < CardPan.Length - 4)
{
next = '*';
}
return value + next;
});
How about replacing a specific matched group using Regex :
string cardNumber = "3456123434561234";
var pattern = "^(.{6})(.+)(.{4})$";
var maskedNumber = Regex.Replace(cardNumber, pattern, (match) =>
{
return Regex.Replace(String.Format("{0}{1}{2}",
match.Groups[1].Value, // the first 6 digits
new String('X', match.Groups[2].Value.Length), // X times the 'X' char
match.Groups[3].Value) /*the last 4 digits*/,".{4}", "$0 "); //finally add a separator every 4 char
});
I would do something like this (pseudo C# - take as rough idea to build upon).
Untested code ahead...
string MaskDigits(string input)
{
//take first 6 characters
string firstPart = input.Substring(0, 6);
//take last 4 characters
int len = input.Length;
string lastPart = input.Substring(len - 4, 4);
//take the middle part (XXXXXXXXX)
int middlePartLenght = input.Substring(6, len - 4).Count();
string middlePart = new String('X', 5);
return firstPart + middlePart + lastPart;
}
Possible implementation (acccepts varios formats e.g. numbers can be divided into groups etc.):
private static String MaskedNumber(String source) {
StringBuilder sb = new StringBuilder(source);
const int skipLeft = 6;
const int skipRight = 4;
int left = -1;
for (int i = 0, c = 0; i < sb.Length; ++i) {
if (Char.IsDigit(sb[i])) {
c += 1;
if (c > skipLeft) {
left = i;
break;
}
}
}
for (int i = sb.Length - 1, c = 0; i >= left; --i)
if (Char.IsDigit(sb[i])) {
c += 1;
if (c > skipRight)
sb[i] = 'X';
}
return sb.ToString();
}
// Tests
// 3456-12XX-XXXX-1234
Console.Write(MaskedNumber("3456-1234-3456-1234"));
// 3456123XXXXX1234
Console.Write(MaskedNumber("3456123434561234"));
this implementation just masks the digits and preserve the format.
Try this one. Simple and straight forward.
public static class StringExtensions
{
public static string Masked(this string source, int start, int count)
{
return source.Masked('x', start, count);
}
public static string Masked(this string source, char maskValue, int start, int count)
{
var firstPart = source.Substring(0, start);
var lastPart = source.Substring(start + count);
var middlePart = new string(maskValue, count);
return firstPart + middlePart + lastPart;
}
}