I recently needed to mask out some characters of a credit card string so that only the last 4 were displayed. Surprisingly there doesn’t seem to be many good examples of this so I decided to put together something myself and I figured it would be more useful as an extension method.

StringExtensions.cs

public static class StringExtensions
{
	public static string Mask(this string source, int start, int maskLength)
	{
		return source.Mask(start, maskLength, 'X');
	}

	public static string Mask(this string source, int start, int maskLength, char maskCharacter)
	{
		if (start > source.Length -1)
		{
			throw new ArgumentException("Start position is greater than string length");
		}

		if (maskLength > source.Length)
		{
			throw new ArgumentException("Mask length is greater than string length");
		}

		if (start + maskLength > source.Length)
		{
			throw new ArgumentException("Start position and mask length imply more characters than are present");
		}

		string mask = new string(maskCharacter, maskLength);
		string unMaskStart = source.Substring(0, start);
		string unMaskEnd = source.Substring(start + maskLength, source.Length - maskLength);

		return unMaskStart + mask + unMaskEnd;
	}
}

IntegerExtensions.cs

public static class IntegerExtensions
{
	public static string ToStringMask(this int source, int start, int maskLength)
	{
		return source.ToString().Mask(start, maskLength, 'X');
	}

	public static string ToStringMask(this int source, int start, int maskLength, char maskCharacter)
	{
		return source.ToString().Mask(start, maskLength, maskCharacter);
	}
}

The below code will output a string with the first 12 characters replaced with “X”s.

string creditCard = "1234567891234567";
string maskedCreditCard = creditCard.Mask(0, 12);
Categories: Programming

Leave a Reply

Your email address will not be published. Required fields are marked *


Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: `POST https://dc.services.visualstudio.com/v2/track` resulted in a `400 Invalid instrumentation key` response: {"itemsReceived":1,"itemsAccepted":0,"errors":[{"index":0,"statusCode":400,"message":"Invalid instrumentation key"}]} in D:\home\site\wwwroot\wp-content\plugins\application-insights\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php:113 Stack trace: #0 D:\home\site\wwwroot\wp-content\plugins\application-insights\vendor\guzzlehttp\guzzle\src\Middleware.php(66): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response)) #1 D:\home\site\wwwroot\wp-content\plugins\application-insights\vendor\guzzlehttp\promises\src\Promise.php(203): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Response)) #2 D:\home\site\wwwroot\wp-content\plugins\application-insights\vendor\guzzlehttp\promises\src\Promise.php(156): GuzzleHttp\Promise\Promise::callHandler(1, Object(GuzzleHttp\P in D:\home\site\wwwroot\wp-content\plugins\application-insights\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php on line 113