Java左右补齐字符长度
Java左右补齐字符长度,如果是数字左补齐0的话可以用:String.format("0x%08X", i); //示例是把整数 i 转换成16进制的数字,不足8位左补0。具体代码如下:
/*** 字符串补齐* @param source 源字符串* @param fillLength 补齐长度* @param fillChar 补齐的字符* @param isLeftFill true为左补齐,false为右补齐* @return*/public static String stringFill(String source, int fillLength, char fillChar, boolean isLeftFill) {if (source == null || source.length() >= fillLength) return source;StringBuilder result = new StringBuilder(fillLength);int len = fillLength - source.length();if (isLeftFill) {for (; len > 0; len--) {result.append(fillChar);}result.append(source);} else {result.append(source);for (; len > 0; len--) {result.append(fillChar);}}return result.toString();}public static String stringFill2(String source, int fillLength, char fillChar, boolean isLeftFill) {if (source == null || source.length() >= fillLength) return source;char[] c = new char[fillLength];char[] s = source.toCharArray();int len = s.length;if(isLeftFill){int fl = fillLength - len;for(int i = 0; i<fl; i++){c[i] = fillChar;}System.arraycopy(s, 0, c, fl, len);}else{System.arraycopy(s, 0, c, 0, len);for(int i = len; i<fillLength; i++){c[i] = fillChar;}}return String.valueOf(c);} 转载请注明诚信在线下载http://www.ttt8888.com