如何格式化带有前导零的 Java 字符串?
例如,这是字符串:
"Apple"
我想添加零来填充 8 个字符:
"000Apple"
我该怎么做?
Here is the String, for example:
"Apple"
and I would like to add zero to fill in 8 chars:
"000Apple"
How can I do so?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(24)
如果您必须在没有库的帮助下完成此操作:(
只要您的字符串不超过 8 个字符,就可以。)
In case you have to do it without the help of a library:
(Works, as long as your String isn't longer than 8 chars.)
这是来自 commons-lang。 参见 javadoc
This is from commons-lang. See javadoc
我相信这就是他真正要求的:
输出:
This is what he was really asking for I believe:
output:
您可以使用另一个答案中使用的 String.format 方法来生成 0 的字符串,
这可以通过动态调整格式字符串中前导 0 的数量来应用于您的问题:
它仍然是一个混乱的解决方案,但具有优势您可以使用整数参数指定结果字符串的总长度。
You can use the String.format method as used in another answer to generate a string of 0's,
This can be applied to your problem by dynamically adjusting the number of leading 0's in a format string:
It's still a messy solution, but has the advantage that you can specify the total length of the resulting string using an integer argument.
你可以使用这个:
You can use this:
使用 Guava 的
Strings
实用程序类:Using Guava's
Strings
utility class:我遇到过类似的情况,我用过这个;它非常简洁,您不必处理长度或其他库。
简单又整洁。字符串格式返回
" Apple"
因此在用零替换空格后,它会给出所需的结果。I've been in a similar situation and I used this; It is quite concise and you don't have to deal with length or another library.
Simple and neat. String format returns
" Apple"
so after replacing space with zeros, it gives the desired result.使用 Apache Commons StringUtils.leftPad(或者查看代码来制作自己的函数)。
复制来源:
Use Apache Commons StringUtils.leftPad (or look at the code to make your own function).
Copied Source:
您可以使用:
这似乎是最简单的方法,不需要任何外部库。
You can use:
It seems to be the simplest method and there is no need of any external library.
在 Java 中:
在 Scala 中:
您还可以在 Java 8 中探索一种使用流和化简来完成此操作的方法(类似于我使用 Scala 的方式)。它与所有其他解决方案有点不同,我特别喜欢它。
In Java:
In Scala:
You can also explore a way in Java 8 to do it using streams and reduce (similar to the way I did it with Scala). It's a bit different to all the other solutions and I particularly like it a lot.
使用 String::repeat 方法的解决方案(Java 11)
如果需要,将 8 更改为另一个数字或将其参数化
Solution with method String::repeat (Java 11)
If needed change 8 to another number or parameterize it
我喜欢 Pad a String with Zeros 的解决方案,
长度 = "8" 和 inputString =“苹果”
I like the solution from Pad a String with Zeros
with length = "8" and inputString = "Apple"
您可能需要处理边缘情况。这是一个通用方法。
You may have to take care of edgecase. This is a generic method.
有没有人尝试过这个纯Java解决方案(没有SpringUtils):
不幸的是,这个解决方案只有在字符串中没有空格的情况下才有效。
Did anyone tried this pure Java solution (without SpringUtils):
Unfortunately this solution works only if you do not have blank spaces in a string.
这很快&适用于任何长度。
This is fast & works for whatever length.
字符串中的大多数字符只有 8 个字符,
可以比 Chris Lercher 回答 更快,因为在我的机器上, 速度快 1/8 。
Can be faster then Chris Lercher answer when most of in String have exacly 8 char
in my case on my machine 1/8 faster.
这是我用来预填充字符串的简单的无 API“可读脚本”版本。 (简单、可读且可调整)。
Here is the simple API-less "readable script" version I use for pre-padding a string. (Simple, Readable, and Adjustable).
如果你想用纯Java编写程序,你可以按照下面的方法,或者有很多String Utils可以帮助你更好地使用更高级的功能。
使用简单的静态方法,您可以实现这一点,如下所示。
您可以使用上述方法
addLeadingText(length, padding text, your text)
输出将为 000Apple
If you want to write the program in pure Java you can follow the below method or there are many String Utils to help you better with more advanced features.
Using a simple static method you can achieve this as below.
You can use the above method
addLeadingText(length, padding text, your text)
The output would be 000Apple
它不漂亮,但很有效。如果您可以访问 apache commons 我建议使用它
It isn't pretty, but it works. If you have access apache commons i would suggest that use that