有一些简单的方法来填充字符串在Java?
似乎是一些应该在一些stringutil类API,但我找不到任何东西,这样做。
有一些简单的方法来填充字符串在Java?
似乎是一些应该在一些stringutil类API,但我找不到任何东西,这样做。
当前回答
概括一下Eko的答案(Java 11+):
public class StringUtils {
public static String padLeft(String s, char fill, int padSize) {
if (padSize < 0) {
var err = "padSize must be >= 0 (was " + padSize + ")";
throw new java.lang.IllegalArgumentException(err);
}
int repeats = Math.max(0, padSize - s.length());
return Character.toString(fill).repeat(repeats) + s;
}
public static String padRight(String s, char fill, int padSize) {
if (padSize < 0) {
var err = "padSize must be >= 0 (was " + padSize + ")";
throw new java.lang.IllegalArgumentException(err);
}
int repeats = Math.max(0, padSize - s.length());
return s + Character.toString(fill).repeat(repeats);
}
public static void main(String[] args) {
System.out.println(padLeft("", 'x', 5)); // => xxxxx
System.out.println(padLeft("1", 'x', 5)); // => xxxx1
System.out.println(padLeft("12", 'x', 5)); // => xxx12
System.out.println(padLeft("123", 'x', 5)); // => xx123
System.out.println(padLeft("1234", 'x', 5)); // => x1234
System.out.println(padLeft("12345", 'x', 5)); // => 12345
System.out.println(padLeft("123456", 'x', 5)); // => 123456
System.out.println(padRight("", 'x', 5)); // => xxxxx
System.out.println(padRight("1", 'x', 5)); // => 1xxxx
System.out.println(padRight("12", 'x', 5)); // => 12xxx
System.out.println(padRight("123", 'x', 5)); // => 123xx
System.out.println(padRight("1234", 'x', 5)); // => 1234x
System.out.println(padRight("12345", 'x', 5)); // => 12345
System.out.println(padRight("123456", 'x', 5)); // => 123456
System.out.println(padRight("1", 'x', -1)); // => throws
}
}
其他回答
Apache StringUtils有几个方法:leftPad, rightPad, center和repeat。
但是请注意,正如其他人在这个回答中提到和演示的那样,JDK中的String.format()和Formatter类是更好的选择。使用它们而不是公共代码。
除了Apache Commons,还请参阅String。格式,应该能够照顾简单的填充(例如与空格)。
public static String LPad(String str, Integer length, char car) {
return (str + String.format("%" + length + "s", "").replace(" ", String.valueOf(car))).substring(0, length);
}
public static String RPad(String str, Integer length, char car) {
return (String.format("%" + length + "s", "").replace(" ", String.valueOf(car)) + str).substring(str.length(), length + str.length());
}
LPad("Hi", 10, 'R') //gives "RRRRRRRRHi"
RPad("Hi", 10, 'R') //gives "HiRRRRRRRR"
RPad("Hi", 10, ' ') //gives "Hi "
RPad("Hi", 1, ' ') //gives "H"
//etc...
填充到10个字符:
String.format("%10s", "foo").replace(' ', '*');
String.format("%-10s", "bar").replace(' ', '*');
String.format("%10s", "longer than 10 chars").replace(' ', '*');
输出:
*******foo
bar*******
longer*than*10*chars
密码字符显示“*”:
String password = "secret123";
String padded = String.format("%"+password.length()+"s", "").replace(' ', '*');
输出与密码字符串长度相同:
secret123
*********
@ck和@Marlon Tarak的答案是唯一使用char[]的答案,对于每秒有几个填充方法调用的应用程序来说,这是最好的方法。然而,它们没有利用任何数组操作优化,而且对我来说有点覆盖;这完全不需要循环。
public static String pad(String source, char fill, int length, boolean right){
if(source.length() > length) return source;
char[] out = new char[length];
if(right){
System.arraycopy(source.toCharArray(), 0, out, 0, source.length());
Arrays.fill(out, source.length(), length, fill);
}else{
int sourceOffset = length - source.length();
System.arraycopy(source.toCharArray(), 0, out, sourceOffset, source.length());
Arrays.fill(out, 0, sourceOffset, fill);
}
return new String(out);
}
简单测试方法:
public static void main(String... args){
System.out.println("012345678901234567890123456789");
System.out.println(pad("cats", ' ', 30, true));
System.out.println(pad("cats", ' ', 30, false));
System.out.println(pad("cats", ' ', 20, false));
System.out.println(pad("cats", '$', 30, true));
System.out.println(pad("too long for your own good, buddy", '#', 30, true));
}
输出:
012345678901234567890123456789
cats
cats
cats
cats$$$$$$$$$$$$$$$$$$$$$$$$$$
too long for your own good, buddy