简体   繁体   English

Java中基于长度的字符串填充

[英]String padding based on length in Java

So, I have two Strings (line and objectCode) 所以,我有两个字符串(line和objectCode)

I want to print line first, and then print a number of spaces based on the length of line and then print objectCode. 我想先打印行,然后根据行长打印一些空格,然后再打印objectCode。 (So that all of the objectCodes are lined up. (以便所有objectCode都对齐。

I tried, but got output like: 我试过了,但输出如下:

0000    FIRST   ST      RETADR,LX    172028
0003            LD      BX,#LENGTH    692028
0006    CLOOP   +JSUB   RDREC    03100000

objectCode being the last numbers in each line (172028), as you can see they are not lined up like I want them to be. objectCode是每行中的最后一个数字(172028),如您所见,它们没有像我希望的那样排列。

So, in essence I want something like: 所以,从本质上讲,我想要这样的东西:

0000    FIRST   ST      RETADR,LX    172028
0003            LD      BX,#LENGTH   692028
0006    CLOOP   +JSUB   RDREC        03100000

I just can't seem to figure out how to get it. 我只是似乎不知道如何获得它。 Thank you. 谢谢。

edit 编辑

What I have tried: 我尝试过的

First try (this is what should have worked): 首先尝试(这应该是可行的):

String write = String.format("%-45s%s", line, objectCode);
fw.write(write + "\n"); //Using a FileWriter

Second I tried (as a last ditch effort): 第二我尝试了(作为最后的努力):

fw.write(line);
int numOfSpaces = 40 - line.length(); //arbitrary number to check if this works
for (int spaces = 0; spaces < numOfSpaces; spaces++) {
    fw.write(" ");
}
fw.write(objectCode);

I figured it would print less spaces for longer line lengths.. But it didn't seem to work. 我认为它可以打印较少的空格以获取更长的行长度。.但是它似乎没有用。

EDIT 编辑

I have figured out the problem but I don't know how to solve it. 我已经找到了问题,但我不知道如何解决。

The problem is that earlier in the program I trimmed each line variable (trimming off the preceding and ending white spaces) so I could get each word in the line by itself. 问题在于,在程序的早期,我修剪了每个行变量(修剪掉前面和结尾的空白),这样我就可以单独获取行中的每个单词。

So, I had: 所以,我有:

line = input.nextLine();
words[] = line.trim().split("\\s+"); //Splitting by white space

I think the trim() method is my problem here... However, I need it in order to do what the program is intended to do. 我认为trim()方法是我的问题……但是,我需要它才能执行程序打算执行的操作。

Alright so I figured it out and wanted to post the answer in case someone else runs into this problem. 好吧,所以我想通了,想发布答案,以防其他人遇到这个问题。

So, like I said the problem was that I was trimming the line variable based on whitespace (in order to get each word in the line separate) : 因此,就像我说的那样,问题是我正在根据空格修剪行变量(以使行中的每个单词分开):

String line = input.next
String[] words = line.trim().split("\\s+"); //Get each word by itself

The problem was that later in the program I wanted to print something after this line. 问题是,在程序的后面,我想在此行之后打印一些内容。 And there was no way to get the proper length of the line, because all of the whitespaces were gone: 由于所有空白都消失了,因此无法获得正确的行长:

0000    FIRST     ST        RETADR,LX  //line.length = 20 (no whitespace)
0003              LD        BX,#LENGTH //line.length = 16 (no whitespace)

Both lines don't take into account any whitespace in their length. 这两行均未考虑其长度中的任何空格。 So when I write something after the line like this (using a filewriter) 所以当我在这样的行之后写东西时(使用文件编写器)

String write = String.format("%-30s, %s", line, objectCode);
fw.write(write + "\n");

I would get : 我会得到:

0000    FIRST     ST        RETADR,LX       172028
0003              LD        BX,#LENGTH            692028

This is because the spaces aren't accounted for when I allocated the 30 spaces for the line (%30s). 这是因为当我为行分配30个空格(%30s)时不考虑空格。

To fix this problem I had to instead of formatting the line itself I would format each word in the line: 为了解决这个问题,我不得不格式化行本身,而不是格式化行本身:

String write = String.format("%-10s%-10s%-10s%-10s%", word1, word2, word3, word4);
fw.write(write + "\t" + objectCode + "\n");

So now this effectively gave me what I wanted, because each line is allocated the same amount of space, and they are left aligned (-10s%,etc..) 所以现在这有效地满足了我的需求,因为每行分配了相同的空间,并且保持对齐(-10s%等)。

0000    FIRST     ST        RETADR,LX   172028
0003              LD        BX,#LENGTH  692028

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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