Java 字符串输出格式

发布于 2024-10-07 01:04:38 字数 298 浏览 3 评论 0原文

我想知道是否有人可以向我展示如何使用 Java 字符串的格式化方法。 例如,如果我希望所有输出的宽度相同

例如,假设我总是希望我的输出相同

Name =              Bob
Age =               27
Occupation =        Student
Status =            Single

在此示例中,所有输出都整齐地格式化在一起;我将如何使用格式方法来实现这一点。

I was wondering if someone can show me how to use the format method for Java Strings.
For instance If I want the width of all my output to be the same

For instance, Suppose I always want my output to be the same

Name =              Bob
Age =               27
Occupation =        Student
Status =            Single

In this example, all the output are neatly formatted under each other; How would I accomplish this with the format method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

雾里花 2024-10-14 01:04:38
System.out.println(String.format("%-20s= %s" , "label", "content" ));
  • 其中 %s 是字符串的占位符。
  • “-”使结果左对齐。
  • 20 是第一个字符串的宽度

输出如下所示:

label               = content

作为参考,我推荐 格式化程序语法的 Javadoc

System.out.println(String.format("%-20s= %s" , "label", "content" ));
  • Where %s is a placeholder for you string.
  • The '-' makes the result left-justified.
  • 20 is the width of the first string

The output looks like this:

label               = content

As a reference I recommend Javadoc on formatter syntax

醉殇 2024-10-14 01:04:38

例如,如果您想要至少 4 个字符,

System.out.println(String.format("%4d", 5));
// Results in "   5", minimum of 4 characters

If you want a minimum of 4 characters, for instance,

System.out.println(String.format("%4d", 5));
// Results in "   5", minimum of 4 characters
拥醉 2024-10-14 01:04:38

要回答您更新的问题,您可以

String[] lines = ("Name =              Bob\n" +
        "Age =               27\n" +
        "Occupation =        Student\n" +
        "Status =            Single").split("\n");

for (String line : lines) {
    String[] parts = line.split(" = +");
    System.out.printf("%-19s %s%n", parts[0] + " =", parts[1]);
}

打印

Name =              Bob
Age =               27
Occupation =        Student
Status =            Single

To answer your updated question you can do

String[] lines = ("Name =              Bob\n" +
        "Age =               27\n" +
        "Occupation =        Student\n" +
        "Status =            Single").split("\n");

for (String line : lines) {
    String[] parts = line.split(" = +");
    System.out.printf("%-19s %s%n", parts[0] + " =", parts[1]);
}

prints

Name =              Bob
Age =               27
Occupation =        Student
Status =            Single
自我难过 2024-10-14 01:04:38

编辑:这是一个非常原始的答案,但我无法删除它,因为它已被接受。请参阅下面的答案以获得更好的解决方案,但

为什么不动态生成一个空白字符串以插入到语句中呢?

因此,如果您希望它们全部从第 50 个字符开始...

String key = "Name =";
String space = "";
for(int i; i<(50-key.length); i++)
{space = space + " ";}
String value = "Bob\n";
System.out.println(key+space+value);

将所有这些放入一个循环中,并在每次迭代之前初始化/设置“键”和“值”变量,这样您就成功了。我也会使用 StringBuilder 类,它更有效。

EDIT: This is an extremely primitive answer but I can't delete it because it was accepted. See the answers below for a better solution though

Why not just generate a whitespace string dynamically to insert into the statement.

So if you want them all to start on the 50th character...

String key = "Name =";
String space = "";
for(int i; i<(50-key.length); i++)
{space = space + " ";}
String value = "Bob\n";
System.out.println(key+space+value);

Put all of that in a loop and initialize/set the "key" and "value" variables before each iteration and you're golden. I would also use the StringBuilder class too which is more efficient.

随风而去 2024-10-14 01:04:38
     @Override
     public String toString() {
          return String.format("%15s /n %15d /n %15s /n %15s", name, age, Occupation, status);
     }
     @Override
     public String toString() {
          return String.format("%15s /n %15d /n %15s /n %15s", name, age, Occupation, status);
     }
-小熊_ 2024-10-14 01:04:38

对于十进制值,您可以使用 DecimalFormat

import java.text.*;

public class DecimalFormatDemo {

   static public void customFormat(String pattern, double value ) {
      DecimalFormat myFormatter = new DecimalFormat(pattern);
      String output = myFormatter.format(value);
      System.out.println(value + "  " + pattern + "  " + output);
   }

   static public void main(String[] args) {

      customFormat("###,###.###", 123456.789);
      customFormat("###.##", 123456.789);
      customFormat("000000.000", 123.78);
      customFormat("$###,###.###", 12345.67);  
   }
}

,输出将为:

123456.789  ###,###.###   123,456.789
123456.789  ###.##        123456.79
123.78      000000.000    000123.780
12345.67    $###,###.###  $12,345.67

有关更多详细信息,请参见此处:

http://docs.oracle.com/javase/tutorial/java/data/numberformat.html

For decimal values you can use DecimalFormat

import java.text.*;

public class DecimalFormatDemo {

   static public void customFormat(String pattern, double value ) {
      DecimalFormat myFormatter = new DecimalFormat(pattern);
      String output = myFormatter.format(value);
      System.out.println(value + "  " + pattern + "  " + output);
   }

   static public void main(String[] args) {

      customFormat("###,###.###", 123456.789);
      customFormat("###.##", 123456.789);
      customFormat("000000.000", 123.78);
      customFormat("$###,###.###", 12345.67);  
   }
}

and output will be:

123456.789  ###,###.###   123,456.789
123456.789  ###.##        123456.79
123.78      000000.000    000123.780
12345.67    $###,###.###  $12,345.67

For more details look here:

http://docs.oracle.com/javase/tutorial/java/data/numberformat.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文