Because we often need to format the data to facilitate communication and data interaction between systems, it is inevitable that the number of digits is insufficient to fill the corresponding data. For example, you want to obtain a 7-bit binary data format, while a 2-digit data format uses 0, 1 as the data signal, and only 1, 0 as the data format, I am talking about 7 bits, which is equivalent to the following: 1000101 format. If my data is 101 three-length binary data, however, I want to return a new method with a fixed length and insufficient bits to be filled with 0.
String SourceStr = "101 ";
String DestinationStr;
DestinationStr = String. PadLeft (7, "0 ");
Console. Write (DestinationStr );
The above code will output: 0000101
Parse this function now. This function has two overloaded versions.
Overload 1: public string PadLeft (int totalWidth );
Overload 2 public string PadLeft (int totalWidth, char paddingChar );
For the explanation of reload 1, Microsoft commented: (this is filled with spaces on the left by default to keep the right aligned .)
//
// Summary:
// Right-aligns the characters in this instance, padding with spaces on
// Left for a specified total length.
//
// Parameters:
// TotalWidth:
// The number of characters in the resulting string, equal to the number
// Original characters plus any additional padding characters.
//
// Returns:
// A new System. String that is equivalent to this instance, but right-aligned
// And padded on the left with as defined spaces as needed to create a length
// TotalWidth. Or, if totalWidth is less than the length of this instance,
// New System. String object that is identical to this instance.
//
// Exceptions:
// System. ArgumentOutOfRangeException:
// TotalWidth is less than zero.
Public string PadLeft (int totalWidth );
The comments of reload 2 are: (you can fill the string based on the characters you want to fill. The alignment is the right alignment of the string .)
Code
//
// Summary:
// Right-aligns the characters in this instance, padding on the left with a
// specified Unicode character for a specified total length.
//
// Parameters:
// totalWidth:
// The number of characters in the resulting string, equal to the number of
// original characters plus any additional padding characters.
//
// paddingChar:
// A Unicode padding character.
//
// Returns:
// A new System.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 System.String that is identical to this instance.
//