Sometimes you may need to show series number along the record to show total number of records, or you may show the size of particular object and compare it with the size of the other object.In case of your serial number for the record, lets says you have total 100 records starting from 1. Then your serial number will be 1,2,3,4 ..... 100. You see that from 1 to 9 serial number consist of one digit and from 10 to 99 it consist of 2 digit and then last is the 100 which consist of 3 digit. In order to have all the serial number of same number of digit you can use String.PadLeft method and give the character to pad. Here is the Pad function of the string which consist of the PadLeft and the PadRight.
- PadLeft:This method right-aligns the characters in a string, padding on the left with spaces or a specified character for a specified total length.
- PadRight:This method left-aligns the characters in a string, padding on the right with spaces or a specified character, for a specified total length.
Here is the code which is used to show you how you can use the PadLeft string function to add 0 (zero) on the left side of the integer value. You can see from the code that I have three integer variables intMaximumValue , intAverageValue and the intMinimumValues which hold the Maximum Values, Average Value and the Minimum Values. Here I have 100, 50 and 1 as Maximum , Average and Minimum values.
In the code above I have used the PadLeft function of the string. Which take the total width and the padding character, Here I have used the intMaximumValue.ToString().Length to calculate the total width for the PadLeft function and I have passed '0' (zero) to be pad on the left side of the value. Here is the output of the above code and you can see after PadLeft all the three value are consist of 3 digits.
When using the PadLeft function it will return new String that is equivalent to this instance, but right-aligned and padded on the left with as many paddingChar characters as needed to create a length of totalWidth. Or, if totalWidth is less than the length of this instance, a new String that is identical to this instance. Here are the detail of the two overloaded function of the PadLeft string function
|PadLeft(Int32)||Right-aligns the characters in this instance, padding with spaces on the left for a specified total length|
|PadLeft(Int32, Char)||Right-aligns the characters in this instance, padding on the left with a specified Unicode character for a specified total length.|
All and any comments / bugs / suggestions are welcomed!