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

前端 未结 9 874
无人及你
无人及你 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:56

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

提交回复
热议问题