Problem Statement:
I had a requirement, to set a field value in the format "ABC/001".
Here ABC was a constant prefix and 001 was an incremental count.
The counter created to store this incremental value takes a single digit( e.g:1) and I needed it to be set as '001', also when the counter reaches 10, the value set should be 010.
Thus, I have to pad variable number of zeros to set Prefix/3 digits
Solution:
In case the value to be incremented in an integer, you need to convert it to string.
string Paddedoutput = counter.ToString();
Paddedoutput = Paddedoutput.PadLeft(3, '0');
Syntax: Output=yourstring.Padleft(no of digits to be maintained,'0')
Similarly, if we need zeros at the end, we can use PadRight(no of digits to be maintained,'0')
Instead of zero, you can add another character as well.
Comments
Post a Comment