Dr. Rajesh Rolen
Dr. Rajesh Rolen

Reputation: 14285

Create a replace function in vb.net

Please Help me in creating a replace function. Problem: Their is a alphanumeric value of any length (string) and I want to replace its all characters with 'X' except right four characters

Like : Value : 4111111111111111 Result Should be: XXXXXXXXXXXX1111

I have created a function but got stuck:

public function myfunction(str as string)
  str.Replace(str.Substring(0, str.Length - 5), 'X') 'but here I want no of x to be equals to count of length of str - 4
end function

What's a better function to perform such an operation?

Upvotes: 0

Views: 487

Answers (3)

Adam Maras
Adam Maras

Reputation: 26843

Try this on for size.

Public Shared Function ObfuscateCardNumber(ByVal cardNumber As String) As String
    If cardNumber.Length <= 4 Then
        Return cardNumber
    Else
        Return cardNumber _
            .Substring(cardNumber.Length - 4, 4) _
            .PadLeft(cardNumber.Length, "X"c)
    End If
End Function

Upvotes: 2

Ivo
Ivo

Reputation: 3436

something like

string result for(int i = 0;i > str.length -4;i++) { result = result +x } result = result + str.substrin(get last 4)

Upvotes: 0

AMissico
AMissico

Reputation: 21684

Dim sNumber As String = "4111111111111111"
Dim sResult As String = StrDup(sNumber.Length - 4, "X"c) + Strings.Right(sNumber, 4)

Upvotes: 1

Related Questions