Mask out part first 12 characters of string with *?

Something like this:

string s = "1234567890123"; // example
string result = s.Substring(s.Length - 4).PadLeft(s.Length, '*');

This will mask all but the last four characters of the string. It assumes that the source string is at least 4 characters long.


using System;

class Program
{
    static void Main()
    {
        var str = "1234567890123456";
        if (str.Length > 4)
        {
            Console.WriteLine(
                string.Concat(
                    "".PadLeft(12, '*'), 
                    str.Substring(str.Length - 4)
                )
            );
        }
        else
        {
            Console.WriteLine(str);
        }
    }
}

Tags:

C#

.Net

C# 4.0