62,614
社区成员
发帖
与我相关
我的任务
分享
String str = "abc";
str = String.format("%1$-1000s", str);
public static void main(String[] args) {
String g = "Ganky";
long l = System.currentTimeMillis();
formatStr(g,10000000,'0');
System.out.println(System.currentTimeMillis()-l);
l = System.currentTimeMillis();
rightPad(g,10000000,'0');
System.out.println(System.currentTimeMillis()-l);
}
public static String rightPad(String text, int length, char c) {
char[] array = new char[length];
System.arraycopy(text.toCharArray(), 0, array, 0, text.length());
Arrays.fill(array, text.length(), length, c);
return new String(array);
}
public static String formatStr(String str, int length, char c) {
StringBuffer sb = new StringBuffer(str);
length = length - str.length();
for(int i=0;i<length;i++) {
sb.append(c);
}
return sb.toString();
}
new String(new char[10]).replace((new char[1])[0], ' ')