如何在Java中填充字符串? [英] How can I pad a String in Java?
本文介绍了如何在Java中填充字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有一些简单的方法可以在Java中填充字符串?
Is there some easy way to pad Strings in Java?
看起来像某些类似于StringUtil的API,但我找不到任何东西这样做。
Seems like something that should be in some StringUtil-like API, but I can't find anything that does this.
推荐答案
Apache StringUtils
有几种方法: leftPad
, rightPad
, 中心
和 重复
。
Apache StringUtils
has several methods: leftPad
, rightPad
, center
and repeat
.
但请注意 - 正如其他人在此答案 - String.format()
和JDK中的 Formatter
类是更好的选择。在公共代码上使用它们。
But please note that — as others have mentioned and demonstrated in this answer — String.format()
and the Formatter
classes in the JDK are better options. Use them over the commons code.
这篇关于如何在Java中填充字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文