我在这里和这里都见过类似的问题。

但我不知道如何左垫字符串与零。

输入:“129018” 输出:“0000129018”

总输出长度应为10。


当前回答

下面是一个基于String的解决方案。适用于字符串的格式,适用于可变长度。

public static String PadLeft(String stringToPad, int padToLength){
    String retValue = null;
    if(stringToPad.length() < padToLength) {
        retValue = String.format("%0" + String.valueOf(padToLength - stringToPad.length()) + "d%s",0,stringToPad);
    }
    else{
        retValue = stringToPad;
    }
    return retValue;
}

public static void main(String[] args) {
    System.out.println("'" + PadLeft("test", 10) + "'");
    System.out.println("'" + PadLeft("test", 3) + "'");
    System.out.println("'" + PadLeft("test", 4) + "'");
    System.out.println("'" + PadLeft("test", 5) + "'");
}

输出: “000000测试” “测试” “测试” “0测试”

其他回答

基于@Haroldo Macêdo的回答,我在自定义Utils类中创建了一个方法,例如

/**
 * 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");

老问题了,但我也有两个方法。


对于固定(预定义的)长度:

    public static String fill(String text) {
        if (text.length() >= 10)
            return text;
        else
            return "0000000000".substring(text.length()) + text;
    }

对于可变长度:

    public static String fill(String text, int size) {
        StringBuilder builder = new StringBuilder(text);
        while (builder.length() < size) {
            builder.append('0');
        }
        return builder.toString();
    }

使用谷歌番石榴:

Maven:

<dependency>
     <artifactId>guava</artifactId>
     <groupId>com.google.guava</groupId>
     <version>14.0.1</version>
</dependency>

示例代码:

Strings.padStart("129018", 10, '0') returns "0000129018"  

如果你的字符串只包含数字,你可以让它成为一个整数,然后做填充:

String.format("%010d", Integer.parseInt(mystring));

如果没有,我想知道如何才能做到。

    int number = -1;
    int holdingDigits = 7;
    System.out.println(String.format("%0"+ holdingDigits +"d", number));

刚刚在采访中问过这个问题........

我的答案如下,但这个(上面提到的)要好得多——>

管柱。format(“% 05d”,全国矿工工会);

我的回答是:

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());
    }
}