public class FixedlengthStr { public static String getFixedLengthStr(String str,int size){ if(size == 0){ size = 10; } StringBuffer sb = new StringBuffer(); if(str.length() == size){ return str; }else if (str.length() < size) { for(int i = 0; i < size - str.length();i++){ sb.append("0"); } sb.append(str); return sb.toString(); }else { return str.substring(0, size); } } public static String getFixedLengthStr(String str){ return getFixedLengthStr(str, 10); } public static void main(String[] args) { String aa = "0123456789"; String bb = "01234567"; String cc = "01234567891010"; System.out.println(aa + " | " + FixedlengthStr.getFixedLengthStr(aa)); System.out.println(bb + " | " + FixedlengthStr.getFixedLengthStr(bb)); System.out.println(cc + " | " + FixedlengthStr.getFixedLengthStr(cc)); } }
获取定长字符串
猜你喜欢
转载自www.cnblogs.com/sangong/p/10561881.html
今日推荐
周排行