本文介紹了我們可以使用 String.format() 用所需長度的字符填充/前綴嗎?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
java.lang.String.format(String str, String str1)
能否用于添加特定字符的前綴.
Can java.lang.String.format(String str, String str1)
be used for adding prefix of a particular character.
我可以為這樣的數字執行此操作:
I could do this for a number like:
int sendID = 202022;
String usiSuffix = String.format("%032d", sendID);
它生成一個長度為 32 并用 0 填充的 String
:00000000000000000000000000202022
It makes a String
of length 32 and leftpadded with 0s : 00000000000000000000000000202022
當 sendID 是 String
時如何實現相同的功能:
How to achieve the same thing when sendID is a String
like:
String sendID = "AABB";
我想要這樣的輸出:0000000000000000000000000000AABB
推薦答案
您可以使用這種駭人聽聞的方式來獲取輸出:
You can use this hackish way to get your output:
String sendID = "AABB";
String output = String.format("%0"+(32-sendID.length())+"d%s", 0, sendID);
這篇關于我們可以使用 String.format() 用所需長度的字符填充/前綴嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!