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

如何在 Java 中使用 String.format() 來復制選項卡“

How to use String.format() in Java to replicate tab quot;quot;?(如何在 Java 中使用 String.format() 來復制選項卡“?)
本文介紹了如何在 Java 中使用 String.format() 來復制選項卡“ "?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在逐行打印數據,并希望它像表格一樣組織.

I'm printing data line by line and want it to be organized like a table.

我最初使用 firstName + ", " + lastName + " " + phoneNumber.

但是對于一些較大的名字,電話號碼會被推到不對齊

But for some of the larger names, the phone number gets pushed out of alignment

我正在嘗試使用 String.format() 來實現此效果.誰能告訴我要使用的格式語法嗎?

I'm trying to use String.format() to achieve this effect. Can anyone tell me the format syntax to use?

我試過 String.format("%s, %s, %20s", firstName, lastName, phoneNumber),但這不是我想要的.我希望它看起來像這樣:

I tried String.format("%s, %s, %20s", firstName, lastName, phoneNumber), but that's not what I want. I want it to look like this:

約翰·史密斯            123456789

John, Smith            123456789

鮑勃,麥迪遜         123456789

Bob, Madison         123456789

查爾斯·理查茲  123456789

Charles, Richards  123456789

這些答案似乎適用于 System.out.println().但我需要它為 JTextArea 工作.我正在使用 textArea.setText()

These answers seem to work for System.out.println(). But I need it to work for a JTextArea. I'm using textArea.setText()

解決了.JTextArea 默認不使用等寬字體.我使用 setFont() 來改變它,現在它就像一個魅力.謝謝大家的解決方案.

Worked it out. JTextArea doesn't use monospaced fonts by default. I used setFont() to change that, and now it works like a charm. Thank you all for the solutions.

推薦答案

考慮使用負數作為長度說明符:%-20s.例如:

consider using a negative number for your length specifier: %-20s. For example:

   public static void main(String[] args) {
     String[] firstNames = {"Pete", "Jon", "Fred"};
     String[] lastNames = {"Klein", "Jones", "Flinstone"};
     String phoneNumber = "555-123-4567";

      for (int i = 0; i < firstNames.length; i++) {
        String foo = String.format("%-20s %s", lastNames[i] + ", " + 
             firstNames[i], phoneNumber);
        System.out.println(foo);
      }   
   }

返回

Klein, Pete          555-123-4567
Jones, Jon           555-123-4567
Flinstone, Fred      555-123-4567

這篇關于如何在 Java 中使用 String.format() 來復制選項卡“ "?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 免费在线观看黄网站 | 国产成人免费在线 | 男人的天堂久久 | 91精品国产综合久久小仙女图片 | 久草在线中文888 | 超碰日本 | 99精品欧美一区二区蜜桃免费 | 精品日韩一区二区 | 成人免费观看男女羞羞视频 | 91原创视频 | 国产精品视频偷伦精品视频 | 国产精品久久久久无码av | 久久国产精品-久久精品 | 美女操网站 | 亚洲欧美在线视频 | 久热久| 久久久久无码国产精品一区 | 91视频国产区 | 九九亚洲| 成人在线中文字幕 | 国产视频二区 | 99综合| 卡通动漫第一页 | 国产美女在线看 | 亚洲综合三区 | 色综合一区二区三区 | 欧美成人精品激情在线观看 | 成人精品一区二区三区 | 日韩中文在线视频 | 日韩精品久久一区二区三区 | 91视频中文| 本道综合精品 | 成人毛片在线视频 | 久久精品在线播放 | 日本网站免费在线观看 | 黄色免费av | 国产成人精品久久久 | 日韩在线 | av网站免费 | 91在线免费视频 | 五月天婷婷丁香 |