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

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

    Try this one. Simple and straight forward.

    public static class StringExtensions
    {
        public static string Masked(this string source, int start, int count)
        {
            return source.Masked('x', start, count);
        }
    
        public static string Masked(this string source, char maskValue, int start, int count)
        {
            var firstPart = source.Substring(0, start);
            var lastPart = source.Substring(start + count);
            var middlePart = new string(maskValue, count);
    
            return firstPart + middlePart + lastPart;
        }
    }
    

提交回复
热议问题