Can we use String.format() to pad/prefix with a character with desired length?

后端 未结 5 762
广开言路
广开言路 2021-01-04 10:13

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:



        
5条回答
  •  醉话见心
    2021-01-04 10:56

    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);
    

    Demo: http://ideone.com/UNVjqS

提交回复
热议问题