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

后端 未结 5 743
广开言路
广开言路 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:38

    This is how I solved this issue using the base jdks String.format function

    String sendId="AABB";
    int length=20;
    String.format("%"+length+"s",sendId).replaceAll(" ","0")
    

提交回复
热议问题