我试图让这个函数返回一个蒙版数字。假设我的号码是123456789.我试图让它返回* 6789如果我给出一个未屏蔽的值4,或者* 789如果我给它一个未屏蔽的值3.目前它显示的是数字中的位数,这就是我试图隐藏的东西。我一直在玩弄这个,但我不能让它做我想做的事。
Public Function GetMaskedNumber(ByVal sNumber As String, ByVal iUnmaskedLength As Integer, ByVal sMaskChar As String) As String
sMaskChar = Trim(sMaskChar)
If iUnmaskedLength > 0 AndAlso Len(sMaskChar) > 0 Then
GetMaskedNumber = New String(sMaskChar(0), iUnmaskedLength)
If iUnmaskedLength < Len(sNumber) Then
Mid(GetMaskedNumber, (Len(sNumber) - iUnmaskedLength), iUnmaskedLength + 1) = Right(sNumber, iUnmaskedLength)
Else
GetMaskedNumber = sNumber
End If
Else
GetMaskedNumber = sNumber
End If
End Function
答案 0 :(得分:3)
如果您想要将数字掩盖为整数(比如iNumber)而不是字符串,则可以使用
"*" & CStr(iNumber mod (10 ^ iUnmaskedLength))
(请注意,在vb.net中^
是指数。)
如果您不需要使用sNumber,请使用
"*" & Right(sNumber, iUnmaskedLength)
Right()
允许iUnmaskedLength
大于字符串的长度;在这种情况下,它返回输入字符串。
答案 1 :(得分:0)
进行一些错误检查。
测试代码
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'test
Dim i As Long = Long.MaxValue
Dim s As String = GetMaskedNumber(i, 12)
s = GetMaskedNumber(i, 20)
End Sub
功能
Public Function GetMaskedNumber(ByVal theNum As Long, _
ByVal UnmaskedLength As Integer, _
Optional MaskChar As String = "*") As String
Dim retval As String = theNum.ToString
If retval.Length > UnmaskedLength Then
retval = String.Format("{0}{1}", MaskChar, retval.Substring(retval.Length - UnmaskedLength))
Else
'ERROR???
Stop
End If
Return retval
End Function