Pad left with zeroes

Viewed 92023

I want to pad left every number with zeroes (it has to be 8 digits) in my string.

e.g.

asd 123 rete > asd 00000123 rete
4444 my text > 00004444 my text

Is it possible to do this using regular expressions? Especially Regex.Replace()?

Notice that the number of zeroes is different for different numbers. I mean the padded number has to be 8 digits long.

4 Answers
static void Main(string[] args)
    {
       string myCC = "4556364607935616";
       string myMasked = Maskify(myCC);
       Console.WriteLine(myMasked);
    }        

public static string Maskify(string cc)
    {
       int len = cc.Length;
       if (len <= 4)
          return cc;

       return cc.Substring(len - 4).PadLeft(len, '#');
    }

Output:

######5616

Just replace the '#' with 0 or '0'. Hope that helped :)

Related