字符串长度不够时 根据字符长度补空格
public static String String_length(String value,int totalLen) {
int valueLength = 0; String chinese = "[\u4e00-\u9fa5]"; for (int i = 0; i < value.length(); i++) { String temp = value.substring(i, i + 1); if (temp.matches(chinese)) { valueLength += 2; } else { valueLength += 1; } } if (valueLength < totalLen) { int temp = totalLen - valueLength; for (int i = 0; i < temp; i++) { value+= " "; } } return value; }
用法 :效果字符串长度不够时 根据字符串长度补空格String.format("%-12s","aaa")