如何在c#中仅显示int的一部分(如切断部分信用卡号)

时间:2022-01-24 17:33:35

If I have a credit number that is an int and I just want to display the last 4 numbers with a * on the left, how would I do this in C#?

如果我有一个int的信用卡号,我只想在左边显示最后4个带*的数字,我将如何在C#中执行此操作?

For example, 4838382023831234 would shown as *1234

例如,4838382023831234将显示为* 1234

7 个解决方案

#1


// assumes that ccNumber is actually a string
string hidden = "*" + ccNumber.Substring(ccNumber.Length - 4);

#2


If it's an integer type?

如果是整数类型?

Where i is the int

我在哪里

string maskedNumber = string.Format("*{0}", i % 10000)

This will get the modulus of 10,000 which will return the last four digits of the int

这将得到10,000的模数,它将返回int的最后四位数

#3


string myCc = myCc.ToString().Substring(12).PadLeft(1, '*');

#4


A credit card number will overflow an int32 and just like phone numbers it doesn't make any sense to think about adding, subtractings, or multiplying credit card numbers. Also string inputs can handle formatting because some users will write in the hyphens. For those reasons, its a lot better to store these objects as strings and reserve numeric value types for data that you actually intend to perform arithmetic on.

信用卡号码将溢出int32,就像电话号码一样,考虑添加,减去或乘以信用卡号码没有任何意义。字符串输入也可以处理格式化,因为有些用户会用连字符写。出于这些原因,将这些对象存储为字符串并为实际打算执行算术运算的数据保留数值类型会好得多。

#5


I'm not satisfied.

我不满意。

binaryworrier: Mind that if you use modulo, you will get fewer digits for numbers such as 1234123412340001

binaryworrier:请注意,如果你使用modulo,你会获得更少的数字,例如1234123412340001

sshow: mind that, if you use substring(12), you will get fewer digits for numbers such as 0000123412341234

sshow:请注意,如果您使用substring(12),您将获得更少的数字,例如0000123412341234

solution would be:

解决方案是:

UInt64 ccNumber;
string s = ccNumber.ToString().Text.PadLeft(15, 'myString');
string last = "*"+s.Substring(s.Length-4);

But on a more abstract note, is a credit card number actually a number? I think not; much more likely that you are going to want to manipulate it digit by digit than perform arithmetic on it. Your advantage of converting char[16] to UInt64 cuts storage space by 50%. No wait, 75% - stupid two-byte-chars!

但从更抽象的角度来看,信用卡号码实际上是一个数字吗?我想不是;更有可能的是,你想要逐位操作它而不是对其进行算术运算。将char [16]转换为UInt64的优势在于将存储空间减少了50%。没有等待,75% - 愚蠢的双字节字符!

#6


If the number is stored as a string then this will do it

如果数字存储为字符串,那么这样做

    string ccNumber = "4242424242424242";
    string modifiedCCNumber = "*" + ccNumber.Substring(ccNumber.Length - 4);

#7


string cardNo = "1234567890123456";
string maskedNo = "*" + cardNo.Substring(12,4);

#1


// assumes that ccNumber is actually a string
string hidden = "*" + ccNumber.Substring(ccNumber.Length - 4);

#2


If it's an integer type?

如果是整数类型?

Where i is the int

我在哪里

string maskedNumber = string.Format("*{0}", i % 10000)

This will get the modulus of 10,000 which will return the last four digits of the int

这将得到10,000的模数,它将返回int的最后四位数

#3


string myCc = myCc.ToString().Substring(12).PadLeft(1, '*');

#4


A credit card number will overflow an int32 and just like phone numbers it doesn't make any sense to think about adding, subtractings, or multiplying credit card numbers. Also string inputs can handle formatting because some users will write in the hyphens. For those reasons, its a lot better to store these objects as strings and reserve numeric value types for data that you actually intend to perform arithmetic on.

信用卡号码将溢出int32,就像电话号码一样,考虑添加,减去或乘以信用卡号码没有任何意义。字符串输入也可以处理格式化,因为有些用户会用连字符写。出于这些原因,将这些对象存储为字符串并为实际打算执行算术运算的数据保留数值类型会好得多。

#5


I'm not satisfied.

我不满意。

binaryworrier: Mind that if you use modulo, you will get fewer digits for numbers such as 1234123412340001

binaryworrier:请注意,如果你使用modulo,你会获得更少的数字,例如1234123412340001

sshow: mind that, if you use substring(12), you will get fewer digits for numbers such as 0000123412341234

sshow:请注意,如果您使用substring(12),您将获得更少的数字,例如0000123412341234

solution would be:

解决方案是:

UInt64 ccNumber;
string s = ccNumber.ToString().Text.PadLeft(15, 'myString');
string last = "*"+s.Substring(s.Length-4);

But on a more abstract note, is a credit card number actually a number? I think not; much more likely that you are going to want to manipulate it digit by digit than perform arithmetic on it. Your advantage of converting char[16] to UInt64 cuts storage space by 50%. No wait, 75% - stupid two-byte-chars!

但从更抽象的角度来看,信用卡号码实际上是一个数字吗?我想不是;更有可能的是,你想要逐位操作它而不是对其进行算术运算。将char [16]转换为UInt64的优势在于将存储空间减少了50%。没有等待,75% - 愚蠢的双字节字符!

#6


If the number is stored as a string then this will do it

如果数字存储为字符串,那么这样做

    string ccNumber = "4242424242424242";
    string modifiedCCNumber = "*" + ccNumber.Substring(ccNumber.Length - 4);

#7


string cardNo = "1234567890123456";
string maskedNo = "*" + cardNo.Substring(12,4);