久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <bdo id='6yP5D'></bdo><ul id='6yP5D'></ul>

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

      <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 class="qwawimqqmiuu" id='6yP5D'><tfoot id='6yP5D'></tfoot><dl id='6yP5D'><fieldset id='6yP5D'></fieldset></dl></div>

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

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

        <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 class="qwawimqqmiuu" id='UL5rE'><tfoot id='UL5rE'></tfoot><dl id='UL5rE'><fieldset id='UL5rE'></fieldset></dl></div>
                • <bdo id='UL5rE'></bdo><ul id='UL5rE'></ul>

                    <tbody id='UL5rE'></tbody>
                  <tfoot id='UL5rE'></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.

                  推薦答案

                  我很快就解決了這個問題.您現(xiàn)在可以在 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模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  How can I detect integer overflow on 32 bits int?(如何檢測 32 位 int 上的整數(shù)溢出?)
                  Local variables before return statements, does it matter?(return 語句之前的局部變量,這有關系嗎?)
                  How to convert Integer to int?(如何將整數(shù)轉換為整數(shù)?)
                  How do I create an int array with randomly shuffled numbers in a given range(如何在給定范圍內(nèi)創(chuàng)建一個隨機打亂數(shù)字的 int 數(shù)組)
                  Inconsistent behavior on java#39;s ==(java的行為不一致==)
                  Why is Java able to store 0xff000000 as an int?(為什么 Java 能夠將 0xff000000 存儲為 int?)
                • <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 class="qwawimqqmiuu" id='s0XG1'><tfoot id='s0XG1'></tfoot><dl id='s0XG1'><fieldset id='s0XG1'></fieldset></dl></div>

                              <tbody id='s0XG1'></tbody>
                          • 主站蜘蛛池模板: 亚洲一区二区免费电影 | 狠狠爱一区二区三区 | 成人免费视频网站在线看 | 色眯眯视频在线观看 | 国产精品久久国产精品 | 香蕉久久av| 超碰97av| 欧美99| 精品国产色 | 国产精品久久久久久久久久东京 | 黄网站涩免费蜜桃网站 | 男女羞羞网站 | 亚洲入口 | 国产欧美精品一区二区三区 | 精品欧美乱码久久久久久 | 精品综合久久 | 精品国产一区二区三区日日嗨 | 精品久久久999 | 国产精品美女久久久久久免费 | 欧美激情一区二区三区 | 毛片大全 | 久久精品一区二区三区四区 | 高清成人av | 第一色在线 | 成人三级视频在线观看 | 婷婷开心激情综合五月天 | 狠狠插狠狠操 | 久久久久久久久久久久久久av | 黄色在线观看网址 | 日韩一区二区在线观看 | 国产精品综合久久 | 激情视频一区 | 国产激情视频在线观看 | 午夜精品福利视频 | 国产精品久久久久久久久免费樱桃 | 一区二区三区四区av | 国产精品一区在线观看你懂的 | 91在线精品播放 | 97精品一区二区 | 不卡视频一区二区三区 | 精品福利在线 |