English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Codice di esempio per aggiungere zeri o spazi vuoti a una stringa Java String

废话不多说了,关键代码如下所示:

package cn.com.songjy;
import java.text.NumberFormat;
//Java 中给数字左边补0
public class NumberFormatTest {
public static void main(String[] args) {
// 待测试数据
int i = 1;
// 得到一个NumberFormat的实例
NumberFormat nf = NumberFormat.getInstance();
// 设置是否使用分组
nf.setGroupingUsed(false);
// 设置最大整数位数
nf.setMaximumIntegerDigits(4);
// 设置最小整数位数
nf.setMinimumIntegerDigits(4);
// 输出测试语句
System.out.println(nf.format(i));
}
}
/** 
* The implementation of automatic zero-padding of numbers to strings in Java. 
* 
*/ 
public class TestStringFormat { 
public static void main(String[] args) { 
int youNumber = 1; 
// 0 代表前面补充0 
// 4 代表长度为4 
// d 代表参数为正数型 
String str = String.format("%04d", youNumber); 
System.out.println(str); // 0001 
} 
}
// 流水号加1后返回,流水号长度为4
private static final String STR_FORMAT = "0000"; 
public static String haoAddOne_2(String liuShuiHao){
Integer intHao = Integer.parseInt(liuShuiHao);
intHao++;
DecimalFormat df = new DecimalFormat(STR_FORMAT);
return df.format(intHao);
}

Well, the above code is the implementation of Java String string padding 0 or space, very good, hope it will be helpful to everyone, if you have any questions, please leave me a message, the editor will reply to everyone in time, and I am very grateful to everyone for their support for the Yanao tutorial website!

Ti potrebbe interessare