Exercise: Data Sanitization Pipeline

Implement an extension method to securely mask sensitive strings.

Problem statement

In financial applications, displaying raw credit card numbers on a user interface is a major security violation. You are building a data sanitization pipeline that must automatically mask the majority of a sensitive string, leaving only the last four characters visible for identification purposes.

Task requirements

  • Evaluate a given string and replace all characters except the final four with asterisks (*).

  • If the string is 4 characters or shorter, return it unmodified.

Constraints

  • You must implement this logic as an extension method for the string type.

  • The method must be placed inside a static class.

  • You must use the this keyword to define the target type of the extension method.

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • Extension methods must be static methods housed inside static classes.

  • The first parameter of your extension method should be this string input.

  • You can use a for loop to iterate through its characters by index.

  • Use string concatenation (+ or +=) to build the masked version of the string piece by piece.

Exercise: Data Sanitization Pipeline

Implement an extension method to securely mask sensitive strings.

Problem statement

In financial applications, displaying raw credit card numbers on a user interface is a major security violation. You are building a data sanitization pipeline that must automatically mask the majority of a sensitive string, leaving only the last four characters visible for identification purposes.

Task requirements

  • Evaluate a given string and replace all characters except the final four with asterisks (*).

  • If the string is 4 characters or shorter, return it unmodified.

Constraints

  • You must implement this logic as an extension method for the string type.

  • The method must be placed inside a static class.

  • You must use the this keyword to define the target type of the extension method.

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • Extension methods must be static methods housed inside static classes.

  • The first parameter of your extension method should be this string input.

  • You can use a for loop to iterate through its characters by index.

  • Use string concatenation (+ or +=) to build the masked version of the string piece by piece.

C# 14.0
namespace Finance.Utilities;
// TODO: Define a static class named StringExtensions
// TODO: Inside the class, define a static extension method named MaskSensitiveData