C # String. PadLeft function, text alignment and fill Solution

Source: Internet
Author: User

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.
//
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.