tags:

views:

79

answers:

4

Please Help me in creating a replace function. Problem: Their is a alfanumeric value of any lenght (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 equvals to count of lenght of str - 4
end function

please tell me a better fuction to perform such a operation

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

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
Adam Maras
I fixed the character literal syntax on line 7, I do apologize for that.
Adam Maras
A: 

something like

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

Ivo
A: 

I got my answer using sql server but i want it to be done in dotnet.

SELECT case when len(cardnum) > 4 then replicate('X',len(cardnum)-4) + RIGHT(cardnum,4) ELSE cardnum END from details
Rajesh Rolen- DotNet Developer
I think *Adam Maras* has given you a good solution.
Chau
yes.. i have marked that as answer
Rajesh Rolen- DotNet Developer