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

  • <legend id='UL5rE'><style id='UL5rE'><dir id='UL5rE'><q id='UL5rE'></q></dir></style></legend>

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

      1. <tfoot id='UL5rE'></tfoot>

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

        时间:2023-09-30
          <bdo id='6yP5D'></bdo><ul id='6yP5D'></ul>

            • <legend id='6yP5D'><style id='6yP5D'><dir id='6yP5D'><q id='6yP5D'></q></dir></style></legend>

                <tbody id='6yP5D'></tbody>

                <tfoot id='6yP5D'></tfoot>
              1. <small id='6yP5D'></small><noframes id='6yP5D'>

                  <i id='6yP5D'><tr id='6yP5D'><dt id='6yP5D'><q id='6yP5D'><span id='6yP5D'><b id='6yP5D'><form id='6yP5D'><ins id='6yP5D'></ins><ul id='6yP5D'></ul><sub id='6yP5D'></sub></form><legend id='6yP5D'></legend><bdo id='6yP5D'><pre id='6yP5D'><center id='6yP5D'></center></pre></bdo></b><th id='6yP5D'></th></span></q></dt></tr></i><div id='6yP5D'><tfoot id='6yP5D'></tfoot><dl id='6yP5D'><fieldset id='6yP5D'></fieldset></dl></div>
                  本文介绍了如何使用 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 使字符串居中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在java中获取给定的日期字符串格式(模式)? 下一篇:将 Java SQL 日期从 yyyy-MM-dd 转换为 dd MMMM yyyy 格式

                  相关文章

                  最新文章

                1. <legend id='s0XG1'><style id='s0XG1'><dir id='s0XG1'><q id='s0XG1'></q></dir></style></legend>
                      <bdo id='s0XG1'></bdo><ul id='s0XG1'></ul>

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

                      <tfoot id='s0XG1'></tfoot>

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