该方式用于判断两个字符串长度大小之差,长度短的在后面用空格补充。用于打印时保持字数时使用。
import java.io.UnsupportedEncodingException;
import org.apache.commons.lang.StringUtils;
public class test {
public static void main(String[] args) {
String before = "12312332dsdfsd232323232";
String after = "wqeqxc阿瑟東weqw";
try {
int beforeLenth = before.getBytes("gbk").length;
int afterLenth = after.getBytes("gbk").length;
int gapSize = beforeLenth - afterLenth;
if (gapSize > 0) {
after = StringUtils.rightPad(after, gapSize + after.length(), " ");
}
if (gapSize < 0) {
System.out.println(Math.abs(gapSize));
before = StringUtils.rightPad(before, Math.abs(gapSize) + before.length(), " ");
}
System.out.println(before);
System.out.println(after);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}