Java 用于始终具有固定长度字符串的实用程序类?

Java 用于始终具有固定长度字符串的实用程序类?,java,string,Java,String,对于我的需求,可能已经有了某种apache或guava实用程序类: 我希望总是创建相同长度的字符串。缺少的字符应使用固定字符向左或向右填充。比如: Utils.filledString(teststring, " ", 5); //would ensure the teststring is always 5 chars long, and if not append whitespace to the right Utils.filledString(teststring, "x", -5);

对于我的需求,可能已经有了某种apache或guava实用程序类:

我希望总是创建相同长度的字符串。缺少的字符应使用固定字符向左或向右填充。比如:

Utils.filledString(teststring, " ", 5); //would ensure the teststring is always 5 chars long, and if not append whitespace to the right
Utils.filledString(teststring, "x", -5); //same as above, but fill the 5 chars left with an x

你明白了,可能已经有了,但我没有找到合适的关键字。

在番石榴项目中已经有了解决方案。
它被称为padEnd/padStart

在guava项目中已经有了一个解决方案。 它被称为padEnd/padStart

看看StringUtils:

看看StringUtils:


String.format是您的朋友。不需要实用程序类。请在此处查看可能重复的文档:String.format是您的朋友。不需要实用类。在这里查看可能重复的文档:这就是我要找的!这就是我要找的!
StringUtils.rightPad("bat", 5, "")    = "bat  "
StringUtils.leftPad("bat", 5, "x")    = "xxbat"