c# maintain specification alignment when writing text with txt

You can use the tab character \t, and the relevant usage methods are as follows (reference: https://www.cnblogs.com/qingergege/p/6104577.html):

When the console outputs data, that is, when using printf(), we often use \t to control the alignment, so as to make the output result more neat and beautiful.

However, sometimes we find that when \t is used in time, the data will be misaligned, which is related to how many spaces \t corresponds to.

The program and running results are given first, and then explained.

Code:

copy code
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char* argv)
{

    printf("123456\t123\t45\n");
    printf("12\t123456\t78\n");
    printf("---------------------------------------\n");
    printf("1234567\t123\t45\n");
    printf("12\t123456\t78\n");
    printf("---------------------------------------\n");
    printf("12345678\t123\t45\n");
    printf("12\t123456\t78\n");
    printf("---------------------------------------\n");
    printf("123456789\t123\t45\n");
    printf("12\t123456\t78\n");
    printf("---------------------------------------\n");
    printf("123456781234\t123\t45\n");
    printf("12\t123456\t78\n");

    return 0;
}
copy code

operation result:

It can be seen that there is a situation where the data cannot be aligned. We found that the number of spaces corresponding to \t is not fixed, not 4 spaces or 8 spaces as we thought.

So what are the rules? Give a simple formula:

Let num = |n-8|%8, where n represents the position occupied by the character preceding \t (the preceding character may also be a conversion description, such as %d, %10d, etc.).

Then the number of spaces corresponding to \t, spaceNum, has

So we see that when \t is preceded by 123456, there are two spaces after it; when \t is preceded by 1234567, there is one space after it, and when the front is exactly 12345678, it is followed by 8 spaces

When the front is 123456781234, the number of spaces behind is |12-8|%8=4


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325179497&siteId=291194637