/**
* Left padding a string with the given character
*
* @param str The string to be padded
* @param length The total fix length of the string
* @param padChar The pad character
* @return The padded string
*/
public static String padLeft(String str, int length, String padChar) {
String pad = "";
for (int i = 0; i < length; i++) {
pad += padChar;
}
return pad.substring(str.length()) + str;
}
然后拨打 Utils.padLeft(str, 10, "0");
3
这是我的解决方案:
String s = Integer.toBinaryString(5); //Convert decimal to binary
int p = 8; //preferred length
for(int g=0,j=s.length();g<p-j;g++, s= "0" + s);
System.out.println(s);
int number = -1;
int holdingDigits = 7;
System.out.println(String.format("%0"+ holdingDigits +"d", number));
刚在接受采访时问过这个问题........
我的答案如下,但这(上面提到)更好 - >
String.format("%05d", num);
我的回答是:
static String leadingZeros(int num, int digitSize) {
//test for capacity being too small.
if (digitSize < String.valueOf(num).length()) {
return "Error : you number " + num + " is higher than the decimal system specified capacity of " + digitSize + " zeros.";
//test for capacity will exactly hold the number.
} else if (digitSize == String.valueOf(num).length()) {
return String.valueOf(num);
//else do something here to calculate if the digitSize will over flow the StringBuilder buffer java.lang.OutOfMemoryError
//else calculate and return string
} else {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < digitSize; i++) {
sb.append("0");
}
sb.append(String.valueOf(num));
return sb.substring(sb.length() - digitSize, sb.length());
}
}
int number=129018;
int requiredLengthAfterPadding=10;
String resultString=Integer.toString(number);
int inputStringLengh=resultString.length();
int diff=requiredLengthAfterPadding-inputStringLengh;
if(inputStringLengh<requiredLengthAfterPadding)
{
resultString=new String(new char[diff]).replace("\0", "0")+number;
}
System.out.println(resultString);
20 回答
您可以使用apache commons StringUtils
http://commons.apache.org/lang/api-2.3/org/apache/commons/lang/StringUtils.html
如果您的字符串仅包含数字,则可以将其设为整数,然后执行填充:
如果没有I would like to know how it can be done .
第二个参数是所需的输出长度
“0”是填充字符
这将填充任何字符串,总宽度为10,而不必担心解析错误:
如果你想要右键:
您可以将“#”字符替换为您要填充的任何字符,重复您希望字符串总宽度的次数 . 例如 . 如果要在左侧添加零,以便整个字符串长度为15个字符:
这比khachik的答案的好处是,它不使用Integer.parseInt,它可以抛出异常(例如,如果要填充的数字太大,如12147483647) . 缺点是,如果你填充的内容已经是int,那么你必须将它转换为String并返回,这是不可取的 .
所以,如果你确定它是一个int,那么khachik的答案很有用 . 如果没有,那么这是一种可能的策略 .
格式化字符串使用
输出:字符串:00000wrwer
第一个参数是要格式化的字符串,第二个参数是所需输出长度的长度,第三个参数是要用于填充字符串的char .
使用链接下载jar
http://commons.apache.org/proper/commons-lang/download_lang.cgi
如果您需要性能并知道字符串的最大大小,请使用以下命令:
请注意最大字符串大小 . 如果它比StringBuffer大,那么你将获得
java.lang.StringIndexOutOfBoundsException
.一个老问题,但我也有两种方法 .
对于固定(预定义)长度:
对于可变长度:
使用Google Guava:
Maven:
Sample code:
我更喜欢这段代码:
然后:
这是另一种方法:
基于@Haroldo Macêdo's answer,我在自定义
Utils
类中创建了一个方法,例如然后拨打
Utils.padLeft(str, 10, "0");
这是我的解决方案:
输出: 00000101
右边填充固定长度-10:String.format(“%1 $ -10s”,“abc”)左边填充固定长度-10:String.format(“%1 $ 10s”,“abc”)
Satish的解决方案在预期答案中非常好 . 我想通过将变量n添加到格式字符串而不是10个字符来使其更加通用 .
这适用于大多数情况
刚在接受采访时问过这个问题........
我的答案如下,但这(上面提到)更好 - >
String.format("%05d", num);
我的回答是:
检查我的代码,它将适用于整数和字符串 .
假设我们的第一个数字是129018.并且我们想要为此添加零,因此最终字符串的长度将为10.为此您可以使用以下代码
这是一个基于String.format的解决方案,它适用于字符串,适用于可变长度 .
Output: '000000test' 'test' 'test' '0test'
我用过这个:
结果:00123
希望对你有帮助!