0

我试图让这个函数返回一个被屏蔽的数字。假设我有号码 123456789。如果我给它一个未屏蔽的值 4,我试图让它返回 *6789,或者如果我给它一个未屏蔽的值 3,则返回 *789。目前它显示数字中的位数,这就是我试图隐藏的。我一直在玩弄这个,但我不能让它做我想做的事。

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
4

2 回答 2

3

如果您有要屏蔽为整数(例如 iNumber)而不是字符串的数字,则可以使用

"*" & CStr(iNumber mod (10 ^ iUnmaskedLength))

(请注意,在 vb.net^中是指数。)

如果您不需要并且需要使用 sNumber,请使用

"*" & Right(sNumber, iUnmaskedLength)

Right()允许iUnmaskedLength大于字符串的长度;在这种情况下,它返回输入字符串。

于 2013-07-09T13:55:58.607 回答
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
于 2013-07-09T14:16:33.630 回答