Leading zero in string
leading zero in string.i.e. string contain hyphen(-).
Example:
input.
1-1-12,
10-2-1,
1-2-3
Output
01-01-12,
10-02-01,
01-02-03
string szInput = "1-1-12";
string szOutput = "";
foreach (string szValue in szInput.Split('-'))
{
if (szValue.Length < 2)
szOutput = szOutput.Trim() == "" ? (szValue.PadLeft(szValue.Length + 1, '0').Trim() + "-") : (szOutput.Trim() + szValue.PadLeft(szValue.Length + 1, '0').Trim() + "-");
else
szOutput = szOutput.Trim() == "" ? (szValue + "-") : (szOutput + szValue + "-");
}
if (szOutput.EndsWith("-"))
szOutput = szOutput.Remove(szOutput.LastIndexOf("-"));
Console.WriteLine(szOutput);