Search code examples
c#substringpadleft

Can't figure out what this SubString.PadLeft is doing


In this code I am debugging, I have this code snipit:

ddlExpYear.SelectedItem.Value.Substring(2).PadLeft(2, '0');

What does this return? I really can't run this too much as it is part of a live credit card application. The DropDownList as you could imagine from the name contains the 4-digit year.

UPDATE: Thanks everyone. I don't do a lot of .NET development so setting up a quick test isn't as quick for me.


Solution

  • It takes the last two digits of the year and pads the left side with zeroes to a maximum of 2 characters. Looks like a "just in case" for expiration years ending in 08, 07, etc., making sure that the leading zero is present.