String.format

String.format("%09d",50)

%d:整形

09:9位,不足补0

private static String template = "%09d";
    private static int[] grids = new int[] {3,2,2,2};
    private static int length = 9;
    
    public static String getAvatar(long key) {
        String r = String.format(template, key);
        StringBuffer buf = new StringBuffer(32);
        
        int pos = 0;
        for (int t: grids) {
            buf.append(r.substring(pos, pos + t));
            pos += t;
            if (pos < length) {
                buf.append('/');
            }
        }
        return buf.toString();
    }

/000/00/00/50

posted @ 2017-12-06 10:39  yangfei969  阅读(319)  评论(0编辑  收藏  举报