Announcement: QQ group: 124766907, if you are in. the NET field has unique insights and profound programming skills. It has expertise in a certain field. You are welcome to join this group. This group already has several MVPs, In the SL ,. NET, BS has accomplished people welcome to the group. If you have more than 4 years of experience, do not add it. This group is pursuing the high-end and top-level products. Thank you.
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
// Specified Unicode character 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.
//
// 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 your 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.
//
// Exceptions:
// System. ArgumentOutOfRangeException:
// TotalWidth is less than zero.
Public string PadLeft (int totalWidth, char paddingChar );
Similarly, the usage of PadRight is similar to that of PadRight, but it is supplemented later or filled with the desired characters.