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