java 中补全高位的0

在写课程实验时遇到了给节点label增加序号的问题。这就需要补全高位的0。

这里用到了String.format()方法。

String类的format()方法用于创建格式化的字符串以及连接多个字符串对象。

String Label = "label";
String newLabel = String.format("%s%03d", Label, 7);//0是前面补全0的标志,3是位数
System.out.println(newLabel);

输出:

label007

 

 

 

同时String.format()方法还有许多种用法

下表总结了String.format()方法支持的标志。y表示该标志对于指定的参数类型是受支持的。

 

FlagGeneralCharacterIntegralFloating PointDate/TimeDescription
'-'yyyyyThe result will be left-justified.
'#'y1-y3y-The result should use a conversion-dependent alternate form
'+'--y4y-The result will always include a sign
'  '--y4y-The result will include a leading space for positive values
'0'--yy-The result will be zero-padded
','--y2y5-The result will include locale-specific grouping separators
'('--y4y5-The result will enclose negative numbers in parentheses

1 Depends on the definition of Formattable.

2 For 'd' conversion only.

3 For 'o''x', and 'X' conversions only.

4 For 'd''o''x', and 'X' conversions applied to BigInteger or 'd' applied to byteByteshortShortint and Integerlong, and Long.

5 For 'e''E''f''g', and 'G' conversions only.

 

具体用法见 Class Formatter

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值