I have a card number as a string, for example:
string ClsCommon.str_CardNumbe r = \"3456123434561234\";
The length of this card number can
Many of the given solutions parse the input multiple times. Below I present a solution that parses the input only once. But I have no experience in C#, so the function is written in Scheme.
The function is divided into two:
(1) visit-first-6 parses the first six characters and concatenates them to the rest of the computation. When visit-first-6 has parsed the first six characters, it calls visit-rest.
(2) visit-rest exploits the fact that we can delay some computation until we have gained more knowledge. In this case, we wait to determine whether the element in the list should be shown until we know how many characters are left.
(define (mask xs)
(letrec ([visit-first-6 (lambda (xs chars-parsed)
(cond
[(null? xs)
;; Shorter than 6 characters.
'()]
[(< chars-parsed 6)
;; Still parsing the first 6 characters
(cons (car xs)
(visit-first-6 (cdr xs)
(1+ chars-parsed)))]
[else
;; The first 6 characters have been parsed.
(visit-rest xs
(lambda (ys chars-left)
ys))]))]
[visit-rest (lambda (xs k)
(if (null? xs)
;; End of input
(k '() 0)
;; Parsing rest of the input
(visit-rest (cdr xs)
(lambda (rest chars-left)
(if (< chars-left 4)
;; Show the last 4 characters
(k (cons (car xs) rest)
(1+ chars-left))
;; Don't show the middle characters
(k (cons "X"
rest)
(1+ chars-left)))))))])
(visit-first-6 xs
0)))
Running mask in the Petite Chez Scheme interpreter
> (mask '(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18))
(1 2 3 4 5 6 "X" "X" "X" "X" "X" "X" "X" "X" 15 16 17 18)
> (mask '())
()
> (mask '(1 2 3 4))
(1 2 3 4)
> (mask '(1 2 3 4 5))
(1 2 3 4 5)
> (mask '(1 2 3 4 5 6 7 8 9))
(1 2 3 4 5 6 7 8 9)
> (mask '(1 2 3 4 5 6 7 8 9 10))
(1 2 3 4 5 6 7 8 9 10)
> (mask '(1 2 3 4 5 6 7 8 9 10 11))
(1 2 3 4 5 6 "X" 8 9 10 11)
NB. I saw this as a funny exercise and I figured I might as well share it. Yannick Meeus has already provided an easily understandable solution. So, this only serves for the interested.
I'm sure there is a cleaner way to do this:
int currentChar = 0;
string maskable = "11111144441111";
string masked = maskable;
int length = masked.Length;
int startMaskPoint = 6;
int endMaskPoint = length - 4 - startMaskPoint;
masked = masked.Remove(startMaskPoint, endMaskPoint);
int numRemoved = length - masked.Length;
string Mask = "";
while (numRemoved != 0)
{
Mask = Mask + "#";
numRemoved--;
}
masked = masked.Insert(startMaskPoint, Mask);
string returnableString = masked;
while (length > 4)
{
returnableString = returnableString.Insert(currentChar + 4, " ");
currentChar = currentChar + 5;
length = length - 4;
}
One method:
string masked = null;
for (int i = 0; i < str_CardNumber.Length; i++) {
masked += (i > 5 && i < str_CardNumber.Length - 4) ? 'X' : str_CardNumber[i];
if ((i + 1) % 4 == 0)
masked += " ";
}