0

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?

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

I fixed the character literal syntax on line 7, I do apologize for that.
1
Dim sNumber As String = "4111111111111111"
Dim sResult As String = StrDup(sNumber.Length - 4, "X"c) + Strings.Right(sNumber, 4)

Comments

0

something like

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.