mask all digits except first 6 and last 4 digits of a string( length varies )

前端 未结 9 831
无人及你
无人及你 2021-01-01 14:26

I have a card number as a string, for example:

string  ClsCommon.str_CardNumbe r = \"3456123434561234\";

The length of this card number can

9条回答
  •  离开以前
    2021-01-01 14:43

    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;
    }
    

提交回复
热议问题