简体   繁体   中英

How do I insert "padding" between two parts of the string?

Let's say I have a string

string totalAmount = "100";
string price = "Price";
ALPHA = Price + totalAmount

I know that one line of the docket can have 42 characters. I want to print this on a docket such that the "Price" is on the left hand side of the docket and the "totalAmount" is on the right hand side.

How would I do this?

PadRight should help you in this case.

string totalAmount = "100";
string price = "Price";
string result = price.PadRight(42 - totalAmount.Length) + totalAmount;

You can use the String.PadLeft / String.PadRight methods of the .net Framework if you use a font like Courier that have the same width for each character.

string totalAmount = "100".
ALPHA = "Price" + totalAmount.PadLeft(37, " ");

You can use String.PadRight and String.PadLeft to achive this.

string totalAmount = "100".PadLeft(37).
ALPHA = "Price" + totalAmount

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM