How to Replace the first x characters of a string with a specific character

Hi everyone, hope anyone of this thread can help me. I have a variable strMasked = 123456789012 , I want to convert the first few characters with “X” and retain the last the four digits as is. The output should be XXXXXXXX9012.

Apparently this is my code strMasked = right(dtExcelValue.Rows(intRowCounter).Item(“Settlement Account No.”).ToString.Replace(“strMasked”,“X”),4)

but the output I’m getting is only 9012. I want the output to be XXXXXXXX9012

Thank you.

Hello @Greggy

You need to use padleft to add x to the value.

strMasked = right(dtExcelValue.Rows(intRowCounter).Item(“Settlement Account No.”).ToString.Replace(“strMasked”,“X”),4)

strMasked=strMasked.padleft(12,“X”)

Thanks

grafik

Strings.Right(dtExcelValue.Rows(intRowCounter).Item("Settlement Account No.").ToString,4).PadLeft(12,"X"c)
1 Like

Thank you so much @ppr for your help, how about if the first few characters are dynamic? like what if the first row has 16 digits while the second row has only 12 and I want the last four to be shown while the first few characters will be replaced by ‘X’?

Thanks @Rahul_Unnikrishnan for your help! :slight_smile:

HI @Greggy

How about this expression?

Strings.Right("123456789",4).PadLeft(CInt("123456789").ToString.count,"X"c)

image

Regards
Gokul

2 Likes

If you don’t have any queries, Kindly close this topic by mark as solved @Greggy

Regards
Gokul

1 Like

as also mentioned, then we make use of the string length from the origin value

strOld = dtExcelValue.Rows(intRowCounter).Item("Settlement Account No.").ToString.Trim
strNew = Strings.Right(strOld ,4).PadLeft(strOld.Length,"X"c)

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.