日期:2014-05-20 浏览次数:20944 次
String padLeft(String s, int length) { byte[] bs = new byte[length]; byte[] ss = s.getBytes(); Arrays.fill(bs, (byte) (48 & 0xff)); System.arraycopy(ss, 0, bs,length - ss.length, ss.length); return new String(bs); }
------解决方案--------------------
自己写一个方法吧:
import java.util.*; public class test{ public static void main(String[] args) { String s = "FF"; System.out.println(padLeft(s,8)); } public static String padLeft(String str,int len){ String pad="0000000000000000"; return len>str.length()&&len<=16&&len>=0?pad.substring(0,len-str.length())+str:str; } }
------解决方案--------------------
private static String fill(String input, int size, char symbol) {
while (input.length() < size) {
input = symbol + input;
}
return input;
}
------解决方案--------------------
public static String pad (String str ,int size ,boolean isprefixed) { if (str == null) str = ""; int str_size = str.length(); int pad_len = size - str_size; StringBuffer retvalue = new StringBuffer(); for (int i = 0; i < pad_len; i++) { retvalue.append("0"); } if (isprefixed) return retvalue.append(str).toString(); return retvalue.insert(0, str).toString(); }