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

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

    How about replacing a specific matched group using Regex :

            string cardNumber = "3456123434561234";
            var pattern = "^(.{6})(.+)(.{4})$";
            var maskedNumber = Regex.Replace(cardNumber, pattern, (match) =>
            {
               return Regex.Replace(String.Format("{0}{1}{2}",
               match.Groups[1].Value, // the first 6 digits
               new String('X', match.Groups[2].Value.Length), // X times the 'X' char
               match.Groups[3].Value) /*the last 4 digits*/,".{4}", "$0 "); //finally add a separator every 4 char
            });
    

提交回复
热议问题