有一些简单的方法来填充字符串在Java?
似乎是一些应该在一些stringutil类API,但我找不到任何东西,这样做。
有一些简单的方法来填充字符串在Java?
似乎是一些应该在一些stringutil类API,但我找不到任何东西,这样做。
当前回答
你可以使用内置的StringBuilder append()和insert()方法, 对于可变字符串长度的填充:
AbstractStringBuilder append(CharSequence s, int start, int end) ;
例如:
private static final String MAX_STRING = " "; //20 spaces
Set<StringBuilder> set= new HashSet<StringBuilder>();
set.add(new StringBuilder("12345678"));
set.add(new StringBuilder("123456789"));
set.add(new StringBuilder("1234567811"));
set.add(new StringBuilder("12345678123"));
set.add(new StringBuilder("1234567812234"));
set.add(new StringBuilder("1234567812222"));
set.add(new StringBuilder("12345678122334"));
for(StringBuilder padMe: set)
padMe.append(MAX_STRING, padMe.length(), MAX_STRING.length());
其他回答
不管怎样,我一直在寻找一些可以填充的东西,然后我决定自己编写代码。它非常简洁,你可以很容易地从中推导出padLeft和padRight
/**
* Pads around a string, both left and right using pad as the template, aligning to the right or left as indicated.
* @param a the string to pad on both left and right
* @param pad the template to pad with, it can be of any size
* @param width the fixed width to output
* @param alignRight if true, when the input string is of odd length, adds an extra pad char to the left, so values are right aligned
* otherwise add an extra pad char to the right. When the input is of even length no extra chars will be inserted
* @return the input param a padded around.
*/
public static String padAround(String a, String pad, int width, boolean alignRight) {
if (pad.length() == 0)
throw new IllegalArgumentException("Pad cannot be an empty string!");
int delta = width - a.length();
if (delta < 1)
return a;
int half = delta / 2;
int remainder = delta % 2;
String padding = pad.repeat(((half+remainder)/pad.length()+1)); // repeating the padding to occupy all possible space
StringBuilder sb = new StringBuilder(width);
// sb.append( padding.substring(0,half + (alignRight ? 0 : remainder)));
sb.append(padding, 0, half + (alignRight ? 0 : remainder));
sb.append(a);
// sb.append( padding.substring(0,half + (alignRight ? remainder : 0)));
sb.append(padding, 0, half + (alignRight ? remainder : 0));
return sb.toString();
}
虽然它应该是相当快的,它可能会受益于使用一些韵母在这里和那里。
你可以使用内置的StringBuilder append()和insert()方法, 对于可变字符串长度的填充:
AbstractStringBuilder append(CharSequence s, int start, int end) ;
例如:
private static final String MAX_STRING = " "; //20 spaces
Set<StringBuilder> set= new HashSet<StringBuilder>();
set.add(new StringBuilder("12345678"));
set.add(new StringBuilder("123456789"));
set.add(new StringBuilder("1234567811"));
set.add(new StringBuilder("12345678123"));
set.add(new StringBuilder("1234567812234"));
set.add(new StringBuilder("1234567812222"));
set.add(new StringBuilder("12345678122334"));
for(StringBuilder padMe: set)
padMe.append(MAX_STRING, padMe.length(), MAX_STRING.length());
这里有一个并行版本的你有很长的字符串:-)
int width = 100;
String s = "129018";
CharSequence padded = IntStream.range(0,width)
.parallel()
.map(i->i-(width-s.length()))
.map(i->i<0 ? '0' :s.charAt(i))
.collect(StringBuilder::new, (sb,c)-> sb.append((char)c), (sb1,sb2)->sb1.append(sb2));
在Dzone上找到的
用零填充:
String.format("|%020d|", 93); // prints: |00000000000000000093|
为此编写自己的函数可能比其他答案更简单。
private static String padRight(String str, String padChar, int n) {
String paddedString = str;
while (paddedString.length() < n) {
paddedString = padChar + str;
}
return paddedString;
}
private static String padLeft(String str, String padChar, int n) {
String paddedString = str;
while (paddedString.length() < n) {
paddedString += padChar;
}
return paddedString;
}