<bdo id='aPDVc'></bdo><ul id='aPDVc'></ul>

      <legend id='aPDVc'><style id='aPDVc'><dir id='aPDVc'><q id='aPDVc'></q></dir></style></legend>
    1. <small id='aPDVc'></small><noframes id='aPDVc'>

      <tfoot id='aPDVc'></tfoot>
        <i id='aPDVc'><tr id='aPDVc'><dt id='aPDVc'><q id='aPDVc'><span id='aPDVc'><b id='aPDVc'><form id='aPDVc'><ins id='aPDVc'></ins><ul id='aPDVc'></ul><sub id='aPDVc'></sub></form><legend id='aPDVc'></legend><bdo id='aPDVc'><pre id='aPDVc'><center id='aPDVc'></center></pre></bdo></b><th id='aPDVc'></th></span></q></dt></tr></i><div id='aPDVc'><tfoot id='aPDVc'></tfoot><dl id='aPDVc'><fieldset id='aPDVc'></fieldset></dl></div>

      1. 如何使用 String.format 使字符串居中?

        How to center a string using String.format?(如何使用 String.format 使字符串居中?)

          <small id='yV9rk'></small><noframes id='yV9rk'>

          • <bdo id='yV9rk'></bdo><ul id='yV9rk'></ul>

                  <tbody id='yV9rk'></tbody>
              1. <i id='yV9rk'><tr id='yV9rk'><dt id='yV9rk'><q id='yV9rk'><span id='yV9rk'><b id='yV9rk'><form id='yV9rk'><ins id='yV9rk'></ins><ul id='yV9rk'></ul><sub id='yV9rk'></sub></form><legend id='yV9rk'></legend><bdo id='yV9rk'><pre id='yV9rk'><center id='yV9rk'></center></pre></bdo></b><th id='yV9rk'></th></span></q></dt></tr></i><div id='yV9rk'><tfoot id='yV9rk'></tfoot><dl id='yV9rk'><fieldset id='yV9rk'></fieldset></dl></div>
                • <legend id='yV9rk'><style id='yV9rk'><dir id='yV9rk'><q id='yV9rk'></q></dir></style></legend>

                  <tfoot id='yV9rk'></tfoot>

                • 本文介绍了如何使用 String.format 使字符串居中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  public class Divers {
                    public static void main(String args[]){
                  
                       String format = "|%1$-10s|%2$-10s|%3$-20s|
                  ";
                       System.out.format(format, "FirstName", "Init.", "LastName");
                       System.out.format(format, "Real", "", "Gagnon");
                       System.out.format(format, "John", "D", "Doe");
                  
                       String ex[] = { "John", "F.", "Kennedy" };
                  
                       System.out.format(String.format(format, (Object[])ex));
                    }
                  }
                  

                  输出:

                  |FirstName |Init.     |LastName            |
                  |Real      |          |Gagnon              |
                  |John      |D         |Doe                 |
                  |John      |F.        |Kennedy             |
                  

                  我希望输出居中.如果我不使用 '-' 标志,输出将向右对齐.

                  I want the output to be centered. If I do not use '-' flag the output will be aligned to the right.

                  我没有在 API 中找到使文本居中的标志.

                  I did not find a flag to center text in the API.

                  这篇文章有一些关于格式的信息,但没有关于中心对齐的信息.

                  This article has some information about format, but nothing on centre justify.

                  推荐答案

                  我很快就解决了这个问题.您现在可以在 String.format 中使用 StringUtils.center(String s, int size).

                  I quickly hacked this up. You can now use StringUtils.center(String s, int size) in String.format.

                  import static org.hamcrest.CoreMatchers.*;
                  import static org.junit.Assert.assertThat;
                  
                  import org.junit.Test;
                  
                  public class TestCenter {
                      @Test
                      public void centersString() {
                          assertThat(StringUtils.center(null, 0), equalTo(null));
                          assertThat(StringUtils.center("foo", 3), is("foo"));
                          assertThat(StringUtils.center("foo", -1), is("foo"));
                          assertThat(StringUtils.center("moon", 10), is("   moon   "));
                          assertThat(StringUtils.center("phone", 14, '*'), is("****phone*****"));
                          assertThat(StringUtils.center("India", 6, '-'), is("India-"));
                          assertThat(StringUtils.center("Eclipse IDE", 21, '*'), is("*****Eclipse IDE*****"));
                      }
                  
                      @Test
                      public void worksWithFormat() {
                          String format = "|%1$-10s|%2$-10s|%3$-20s|
                  ";
                          assertThat(String.format(format, StringUtils.center("FirstName", 10), StringUtils.center("Init.", 10), StringUtils.center("LastName", 20)),
                                  is("|FirstName |  Init.   |      LastName      |
                  "));
                      }
                  }
                  
                  class StringUtils {
                  
                      public static String center(String s, int size) {
                          return center(s, size, ' ');
                      }
                  
                      public static String center(String s, int size, char pad) {
                          if (s == null || size <= s.length())
                              return s;
                  
                          StringBuilder sb = new StringBuilder(size);
                          for (int i = 0; i < (size - s.length()) / 2; i++) {
                              sb.append(pad);
                          }
                          sb.append(s);
                          while (sb.length() < size) {
                              sb.append(pad);
                          }
                          return sb.toString();
                      }
                  }
                  

                  这篇关于如何使用 String.format 使字符串居中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)

                        <tbody id='vf6Mf'></tbody>

                      <i id='vf6Mf'><tr id='vf6Mf'><dt id='vf6Mf'><q id='vf6Mf'><span id='vf6Mf'><b id='vf6Mf'><form id='vf6Mf'><ins id='vf6Mf'></ins><ul id='vf6Mf'></ul><sub id='vf6Mf'></sub></form><legend id='vf6Mf'></legend><bdo id='vf6Mf'><pre id='vf6Mf'><center id='vf6Mf'></center></pre></bdo></b><th id='vf6Mf'></th></span></q></dt></tr></i><div id='vf6Mf'><tfoot id='vf6Mf'></tfoot><dl id='vf6Mf'><fieldset id='vf6Mf'></fieldset></dl></div>
                        <legend id='vf6Mf'><style id='vf6Mf'><dir id='vf6Mf'><q id='vf6Mf'></q></dir></style></legend>
                      • <small id='vf6Mf'></small><noframes id='vf6Mf'>

                          <bdo id='vf6Mf'></bdo><ul id='vf6Mf'></ul>
                          <tfoot id='vf6Mf'></tfoot>