Answers:
如果您必须在没有库帮助的情况下执行此操作:
("00000000" + "Apple").substring("Apple".length())(有效,只要您的字符串不超过8个字符。)
("0000" + theString).substring(theString.length())更现实。这theString用前导零填充。抱歉无法拒绝添加此评论:)
                    StringBuilder,并且需要一个循环来填充它。如果确实只要求截断8个字符的字符串是可以的,尽管这是一个非常狭窄的用例,但是这种解决方案永远不能被称为快速解决方案。很短。
                    public class LeadingZerosExample {
    public static void main(String[] args) {
       int number = 1500;
       // String format below will add leading zeros (the %0 syntax) 
       // to the number above. 
       // The length of the formatted string will be 7 characters.
       String formatted = String.format("%07d", number);
       System.out.println("Number with leading zeros: " + formatted);
    }
}%07s  代替%07d,您将获得一个FormatFlagsConversionMismatchException。你可以试试。
                     StringUtils.leftPad(yourString, 8, '0');这是来自commons-lang。见javadoc
我坚信这就是他真正要的:
String.format("%0"+ (8 - "Apple".length() )+"d%s",0 ,"Apple"); 输出:
000AppleDuplicateFormatFlagsException,则将抛出(由于00格式字符串中的)。如果替换长度超过8个字符的字符串,则会抛出一个IllegalFormatFlagsException(由于负数)。
                    String.format("%0"+ (9 - "Apple".length() )+"d%s",0 ,"Apple").substring(0,8); 。现在您将不会有此异常。
                    String.format("%0"+ (9 - "Apple".length() )+"d%s",0 ,"Apple").substring(0,8);是错误的,应该是String.format("%0"+ (9 - "Apple".length() )+"d%s",0 ,"Apple").substring(1,9); 。
                    您可以使用另一个答案中使用的String.format方法来生成字符串0。
String.format("%0"+length+"d",0)通过动态调整格式字符串中前导0的数量,可以将其应用于您的问题:
public String leadingZeros(String s, int length) {
     if (s.length() >= length) return s;
     else return String.format("%0" + (length-s.length()) + "d%s", 0, s);
}它仍然是一个麻烦的解决方案,但具有的优势是您可以使用整数参数指定结果字符串的总长度。
使用Guava的Strings实用程序类:
Strings.padStart("Apple", 8, '0');您可以使用此:
org.apache.commons.lang.StringUtils.leftPad("Apple", 8, "0")我也遇到过类似的情况,并且使用了它。它非常简洁,您不必处理长度或其他库。
String str = String.format("%8s","Apple");
str = str.replace(' ','0');简单而整洁。字符串格式返回,"   Apple"因此在将空格替换为零后,它会提供所需的结果。
String input = "Apple";
StringBuffer buf = new StringBuffer(input);
while (buf.length() < 8) {
  buf.insert(0, '0');
}
String output = buf.toString();使用Apache Commons StringUtils.leftPad(或查看代码以创建自己的函数)。
public class PaddingLeft {
    public static void main(String[] args) {
        String input = "Apple";
        String result = "00000000" + input;
        int length = result.length();
        result = result.substring(length - 8, length);
        System.out.println(result);
    }
}public static void main(String[] args)
{
    String stringForTest = "Apple";
    int requiredLengthAfterPadding = 8;
    int inputStringLengh = stringForTest.length();
    int diff = requiredLengthAfterPadding - inputStringLengh;
    if (inputStringLengh < requiredLengthAfterPadding)
    {
        stringForTest = new String(new char[diff]).replace("\0", "0")+ stringForTest;
    }
    System.out.println(stringForTest);
}public static String lpad(String str, int requiredLength, char padChar) {
    if (str.length() > requiredLength) {
        return str;
    } else {
        return new String(new char[requiredLength - str.length()]).replace('\0', padChar) + str;
    }
}可以更快地使Chris Lercher回答,当String中的大多数都具有8个字符时
int length = in.length();
return length == 8 ? in : ("00000000" + in).substring(length);就我而言,在我的机器上快1/8。
是否有人尝试过这种纯Java解决方案(没有SpringUtils):
//decimal to hex string 1=> 01, 10=>0A,..
String.format("%1$2s", Integer.toString(1,16) ).replace(" ","0");
//reply to original question, string with leading zeros. 
//first generates a 10 char long string with leading spaces, and then spaces are
//replaced by a zero string. 
String.format("%1$10s", "mystring" ).replace(" ","0");不幸的是,仅当字符串中没有空格时,此解决方案才有效。
如果要使用纯Java编写程序,则可以遵循以下方法,或者有许多String Utils可以帮助您更好地使用更高级的功能。
使用简单的静态方法,可以实现以下目的。
public static String addLeadingText(int length, String pad, String value) {
    String text = value;
    for (int x = 0; x < length - value.length(); x++) text = pad + text;
    return text;
}您可以使用上述方法 addLeadingText(length, padding text, your text)
addLeadingText(8, "0", "Apple");输出将为000Apple