这是字符串,例如:
"Apple"
我想加零来填充 8 个字符:
"000Apple"
我该怎么做?
原文由 Roy 发布,翻译遵循 CC BY-SA 4.0 许可协议
这是字符串,例如:
"Apple"
我想加零来填充 8 个字符:
"000Apple"
我该怎么做?
原文由 Roy 发布,翻译遵循 CC BY-SA 4.0 许可协议
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);
}
}
原文由 Alex Rashkov 发布,翻译遵循 CC BY-SA 3.0 许可协议
4 回答1.1k 阅读✓ 已解决
4 回答795 阅读✓ 已解决
1 回答2.5k 阅读✓ 已解决
1 回答2.8k 阅读✓ 已解决
1 回答1.1k 阅读✓ 已解决
2 回答661 阅读✓ 已解决
2 回答1.6k 阅读
如果您必须在没有图书馆帮助的情况下这样做:
(只要您的字符串不超过 8 个字符,就可以工作。)