屏蔽除字符串的前6位和后4位之外的所有数字(长度变化) [英] mask all digits except first 6 and last 4 digits of a string( length varies )
问题描述
我有一个卡号作为字符串,例如:
I have a card number as a string, for example:
string ClsCommon.str_CardNumbe r = "3456123434561234";
此卡号的长度可以从16到19位数字不等,具体取决于要求。
The length of this card number can vary from 16 to 19 digits, depending on the requirement.
我的要求是,我必须显示卡号的前六位数字和后四位数字,并用'X'掩盖中间的其他字符。
My requirement is that I have to show the first six digits and the last 4 digits of a card number and mask the other characters in between with the character 'X'.
我尝试使用subString并将其分别实现为16、17、18、19位数字。
I have tried using subString and implemented it separately for 16,17,18,19 digits..
我拆分了字符串(ClsCommon.str_CardNumber)到5个字符串(str_cardNum1,str_cardNum2,str_cardNum3,str_cardNum4,str_cardNum5-每个字符串4个数字。.第5个字符串的剩余数字)
I split string(ClsCommon.str_CardNumber) to 5 strings (str_cardNum1, str_cardNum2, str_cardNum3, str_cardNum4, str_cardNum5 - 4 digits for each string..remaining digits for 5th string)
所有字符串放置在ClsCommon文件中。
基于此,我实现了下面的方法,该方法非常有效:
All the strings are placed in ClsCommon file. Based on that I implemented the below, which works perfectly:
if (ClsCommon.str_CardNumber.Length == 16) {
txtmskcrdnum.Text = string.Concat(ClsCommon.str_cardNum1, " ", ClsCommon.str_cardNum2.Substring(0, 2), "XX", " ", "XXXX", " ", ClsCommon.str_cardNum4);
}
if (ClsCommon.str_CardNumber.Length == 17) {
txtmskcrdnum.Text = string.Concat(ClsCommon.str_cardNum1, " ", ClsCommon.str_cardNum2.Substring(0, 2), "XX", " ", "XXXX", " ", "X", ClsCommon.str_cardNum4.Substring(1, 3), " ", ClsCommon.str_cardNum5);
}
if (ClsCommon.str_CardNumber.Length == 18) {
txtmskcrdnum.Text = string.Concat(ClsCommon.str_cardNum1, " ", ClsCommon.str_cardNum2.Substring(0, 2), "XX", " ", "XXXX", " ", "XX", ClsCommon.str_cardNum4.Substring(2, 2), " ", ClsCommon.str_cardNum5);
}
if (ClsCommon.str_CardNumber.Length == 19) {
txtmskcrdnum.Text = string.Concat(ClsCommon.str_cardNum1, " ", ClsCommon.str_cardNum2.Substring(0, 2), "XX", " ", "XXXX", " ", "XXX", ClsCommon.str_cardNum4.Substring(3, 1), " ", ClsCommon.str_cardNum5);
}
txtmskcrdnum.Text = ClsCommon.str_CardNumber.PadLeft(ClsCommon.str_CardNumber.Length, 'X').Substring(ClsCommon.str_CardNumber.Length - 4);
对于多个长度,上述方法没有用。
For multiple lengths, the above approach is not useful.
我想要一种显示前6位和后4位并用X掩盖其他数字的方法。
最后一个字符串应在每4位之间留一个空格。
I want a single approach which displays the first 6 and last 4 digits and masks other digits with X. The final string should have a space between every 4 digits.
推荐答案
这将适用于任何卡号长度:
This will work with any card number length:
var cardNumber = "3456123434561234";
var firstDigits = cardNumber.Substring(0, 6);
var lastDigits = cardNumber.Substring(cardNumber.Length - 4, 4);
var requiredMask = new String('X', cardNumber.Length - firstDigits.Length - lastDigits.Length);
var maskedString = string.Concat(firstDigits, requiredMask, lastDigits);
var maskedCardNumberWithSpaces = Regex.Replace(maskedString, ".{4}", "$0 ");
这篇关于屏蔽除字符串的前6位和后4位之外的所有数字(长度变化)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!