public static String pad(String str, int size, char padChar)
{
if (str.length() < size)
{
char[] temp = new char[size];
int i = 0;
while (i < str.length())
{
temp[i] = str.charAt(i);
i++;
}
while (i < size)
{
temp[i] = padChar;
i++;
}
str = new String(temp);
}
return str;
}
格式化程序解决方案不是最佳的。仅仅构建格式字符串就会创建2个新字符串。
Apache的解决方案可以通过用目标大小初始化sb来改进,从而替换下面的内容
StringBuffer padded = new StringBuffer(str);
与
StringBuffer padded = new StringBuffer(pad);
padded.append(value);
// Get your data from wherever.
final byte[] data = getData();
// Get the digest engine.
final MessageDigest md5= MessageDigest.getInstance("MD5");
// Send your data through it.
md5.update(data);
// Parse the data as a positive BigInteger.
final BigInteger digest = new BigInteger(1,md5.digest());
// Pad the digest with blanks, 32 wide.
String hex = String.format(
// See: http://download.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html
// Format: %[argument_index$][flags][width]conversion
// Conversion: 'x', 'X' integral The result is formatted as a hexadecimal integer
"%1$32x",
digest
);
// Replace the blank padding with 0s.
hex = hex.replace(" ","0");
System.out.println(hex);
public static String padLeft(String in, int size, char padChar) {
if (in.length() <= size) {
char[] temp = new char[size];
/* Llenado Array con el padChar*/
for(int i =0;i<size;i++){
temp[i]= padChar;
}
int posIniTemp = size-in.length();
for(int i=0;i<in.length();i++){
temp[posIniTemp]=in.charAt(i);
posIniTemp++;
}
return new String(temp);
}
return "";
}
// put the number of spaces, or any character you like, in your paddedString
String paddedString = "--------------------";
String myStringToBePadded = "I like donuts";
myStringToBePadded = myStringToBePadded + paddedString.substring(myStringToBePadded.length());
//result:
myStringToBePadded = "I like donuts-------";
public static String padRight(String s, int n, char padding){
StringBuilder builder = new StringBuilder(s.length() + n);
builder.append(s);
for(int i = 0; i < n; i++){
builder.append(padding);
}
return builder.toString();
}
public static String padLeft(String s, int n, char padding) {
StringBuilder builder = new StringBuilder(s.length() + n);
for(int i = 0; i < n; i++){
builder.append(Character.toString(padding));
}
return builder.append(s).toString();
}
public static String pad(String s, int n, char padding){
StringBuilder pad = new StringBuilder(s.length() + n * 2);
StringBuilder value = new StringBuilder(n);
for(int i = 0; i < n; i++){
pad.append(padding);
}
return value.append(pad).append(s).append(pad).toString();
}
/**
* 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();
}