簡體   English   中英

String.format不能完全正常工作?

[英]String.format not completely working?

TMP = String.format("%-25s %25s", STRING1, STRING2);

每當我打印TMP時,似乎都無法按照我想要的方式正確打印表格

例如:

STRING1 = Test1&Test12 STRING2 =你好

Print:
Test1           Hello
Test12           Hello

為什么會這樣呢?

"Test12""Test1"多1個字符。 嘗試這個

String.format("%-25s\t%25s", string1, string2);

您的字符串很可能已經填充了空格

for (String s1 : "Test1,Test12,Test123,Test1234,Test12                    ".split(",")) {
    String tmp = String.format("%-25s %25s", s1, "hello");
    System.out.println(tmp);
}

版畫

Test1                                         hello
Test12                                        hello
Test123                                       hello
Test1234                                      hello
Test12                                         hello

您可以在嘗試格式化它們之前trim()它們。

String tmp = String.format("%-25s %25s", s1.trim(), s2.trim());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM