3
使用Java字符串格式格式化整数
我想知道是否有可能在Java中使用String.format方法给出一个在零之前的整数? 例如: 1将变为001 2将变为002 ... 11将变为011 12将变为012 ... 526将保留为526 ... 目前,我已经尝试了以下代码: String imageName = "_%3d" + "_%s"; for( int i = 0; i < 1000; i++ ){ System.out.println( String.format( imageName, i, "foo" ) ); } 不幸的是,它在数字前带有3个空格。可以在数字前面加上零吗?