Recently, I came across a requirement to mask the string to show only the first letter of each word, and the rest of all characters (including spaces) will be masked.
I utilized the regular expressions to split each word and Linq to mask them.
// See https://aka.ms/new-console-template for more information
using System.Text.RegularExpressions;
using System.Text;
string theName = "First Name, Last Name";
//\S (shows any non spaces characters) \s shows any spaces
// \S+\s+ one or more occurrences of any non space character and then one or more occurrances of a space - This will capture all the words along with the trailing spaces (except the last one where space is not following the word)
//\S+\b+ this is to capture the last word (\b) is the word boundry
var matches = Regex.Matches(theName, @"(\S+\s+|\S+\b+)");
//we are masking each word to retain only the first character as original and rest all to be replaced with 'x' character
var outName = string.Concat(matches.Select(m => m.Value.Substring(0,1) + "".PadRight(m.Value.Length-1,'x')));
Console.WriteLine(outName);
Note that there is no main method and only the statements in the code. This was the new feature MS introduced with C#9 called Top-Level statements.
The output of the code