-6

How to mask first two letter and last four letters of credit card number. I am able to do the last four digits but the first two digits I can't.

I am using following code:

string result = s.Substring(s.Length - 4).PadLeft(4, '*');

Please let me know the best practice.

Brandon
  • 68,708
  • 30
  • 194
  • 223
Kokirala Sudheer
  • 429
  • 2
  • 8
  • 20
  • 2
    Of what Ive seen in the real world, masking software usually mask all but the last four digits of the creditcard nunber. – Mattias Åslund May 23 '14 at 15:45
  • You could use a sticky note. Or are you trying to use some kind of software? What kind? – Smandoli May 23 '14 at 15:45
  • Are you sure you're able to do the last 4 digits? Because I don't think that code is doing what you want it to do. The first argument in PadLeft is the totalWidth, which means your result is going to be 4 characters. – Brandon May 23 '14 at 15:49

1 Answers1

1

You need to cut the text in the string after the first two characters and up to the last four, and then put two asterisks in front and four at the end.

Substring() is an excellent string function for this, combinef with knowing the length of the string, which Length will give you. Thus, a working code snippet would be:

var middle = s.Substring(2, s.Length - 2 - 4);
var result = string.Format("**{0}****", middle);
Mattias Åslund
  • 3,877
  • 2
  • 18
  • 17
  • 3
    This is a low-quality answer. You should explain the OP what you are doing here. **Consider editing it!** – Victor May 23 '14 at 16:06
  • 1
    I just thought the answer was self-describing. Ill edit it for you. – Mattias Åslund May 23 '14 at 17:39
  • 1
    Though this may be what the questioner was asking for, its absolutely terrifyingly incorrect for PCI-DSS payment standards. Strongly recommend no-one uses this. – PaulG May 24 '14 at 19:49