0

This is my code:

if (e.ColumnIndex == 1 && e.Value != null) 
{
    e.Value = new String('*', e.Value.ToString().Length - 4);
}

The result of credit cardNo is ****************

I want to the result to be ************1234

How to do it?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Claris -
  • 11
  • 4
  • You're only assigning `e.Value` a bunch of stars (`new String(...) `). I'm guessing you also want to _append_ (also called _string concatination_) the last four digits of the card number afterwards. – gunr2171 Jun 07 '21 at 16:00
  • 1
    `e.Value = new String('*', e.Value.ToString().Length - 4) + e.Value.Substring(e.Value.Length - 4);` – MaartenDev Jun 07 '21 at 16:02
  • 1
    @MaartenDev your method is very work for me thanks ! Also thanks you guys to help me – Claris - Jun 07 '21 at 16:06

1 Answers1

-1
var cardNo = e.Value.ToString();
var maskedCarNo = new String('*', cardNo.Length - 4) + cardNo[..4];
  • `new String('*', str.Length - 4) + str[^4..];` I believe is what you're looking for here. – Trevor Jun 07 '21 at 16:29
  • 2
    While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – gunr2171 Jun 07 '21 at 16:38
  • @gunr2171 please don't hesitate to edit the answer if you believe in yourself that you can improve it for future ones. – Soner from The Ottoman Empire Jun 07 '21 at 16:40
  • @snr: You're the one with subject matter expertise here, who is putting forward a solution. gunr2171 is simply highlighting that your answer would be far more useful to both the OP as well as future readers if you took the time to add an explanation. That might be especially true here when using relatively new C# syntax that the OP may not be familiar with. – Jeremy Caney Jun 07 '21 at 19:03