Question

I have a bank account number that is variable length.

1234567890123
0987654321

I want to mask the account number for viewing, except for the last 4 digits.

XXXXXXXXX0123
XXXXXX4321

Is there a way to make it mask all the numbers but the last 4 using String.Format without using Substring to pull out the last 4?

EDIT:

There's nothing wrong with substring. I'm just looking to see if there is a simple formatting string to convert all but the last 4 characters into something else.

Was it helpful?

Solution

What's wrong with using substring?

However, if you put the number in a long (needed to hold all the digits), you could do number % 10000 to extract the last four digits, and a fixed string for the XXX...

Answering the literal wording of the question, rather than what you probably really meant :) you could implement a custom formatter to printed masked account numbers, and then there would be a 'simple format string' that printed things in the way you want

The second example on this page formats (all the digits of) an account number. It could be modified to print a masked version instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top