The output characters are aligned to the right, and the output characters are aligned to the right.

Source: Internet
Author: User

The output characters are aligned to the right, and the output characters are aligned to the right.

Let's take a look at the following group of characters. If the output is used, it cannot be right aligned:

 

String [] s1 = {"300", "5", "54210", "6300", "88"}; foreach (string s in s1) {string s2 = s; console. writeLine (s2);} Source Code

 

The C # Processing string has a method, PadLeft (),

 

String [] s1 = {"300", "5", "54210", "6300", "88"}; foreach (string s in s1) {string s2 = s. padLeft (7, ''); Console. writeLine (s2);} Source Code

 

There is a number 7 on the program, and a dead value is given. If the length of an element in the array is greater than this value, the output result cannot be aligned. Next, Insus. NET adds several lines of code to solve this problem. First, it calculates the length of the element in the array element and obtains the longest length value.



String [] s1 = {"300", "5", "54210", "6300", "88"}; int len = 0; foreach (string s in s1) {if (len <s. length) len = s. length;} foreach (string s in s1) {string s2 = s. padLeft (len, ''); Console. writeLine (s2);} Source Code

 

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.