1. Introduction
1.导言
In a Java-based text application, it is often necessary to center on text. Additionally, by centering the text while developing an application for Xbox, a command line tool, or any other application that involves improving the visual representation of text while user inputs, the user is likely to have a good experience in using that program.
在基于 Java 的文本应用程序中,通常需要将文本居中。此外,在开发 Xbox 应用程序、命令行工具或任何其他涉及在用户输入的同时改进文本的可视化表示的应用程序时,通过将文本居中,用户可能会在使用该程序时获得良好的体验。
In this tutorial, we’ll dive into different methods to center-align text output in Java.
在本教程中,我们将深入探讨在 Java 中将文本输出居中对齐的不同方法。
2. Using String Formatting
2.使用字符串格式化
The fastest way to center text in Java can be provided by using the String class’s format() method. Let’s see the following example:
在 Java 中,使用 String 类的 format() 方法可以最快地将文本居中。让我们看看下面的示例:
@Test
public void givenTextAndTotalWidth_whenUsingStringFormat_thenTextIsCentered() {
String text = "Centered Text";
int totalWidth = 15;
int padding = (totalWidth - text.length()) / 2;
String centeredText = String.format("%" + padding + "s%s%" + padding + "s", "", text, "");
Assert.assertEquals(" Centered Text ", centeredText);
}
In this test method, we’d like to display “Centered Text” within the total width of 20 characters. Hence, we calculate how many spacings are needed for both sides. Then, we fill up the required number of strings that have a length equal to the number of spacings by using a method called String.format() to set the right amount of characters.
在此测试方法中,我们希望在 20 个字符的总宽度内显示”居中文本“。因此,我们要计算两边需要多少间距。然后,我们使用名为 String.format() 的方法来设置合适的字符数,从而填充长度等于间距数的字符串。
3. Using StringBuilder
3.使用 StringBuilder
Another way to center on building text is through StringBuilder. It enables more alignment tweaking, hence being more flexible. Let’s take the following example:
另一种以构建文本为中心的方法是通过 StringBuilder 来实现。它可以进行更多的对齐调整,因此更加灵活。让我们以下面的示例为例:
@Test
public void givenTextAndTotalWidth_whenCenterUsingStringBuilder_thenTextIsCentered() {
String text = "Centered Text";
int width = 15;
int padding = (width - text.length()) / 2;
StringBuilder centeredText = new StringBuilder();
for (int i = 0; i < padding; i++) {
centeredText.append(" ");
}
centeredText.append(text);
for (int i = 0; i < padding; i++) {
centeredText.append(" ");
}
String centeredTextString = centeredText.toString();
assertEquals(" Centered Text ", centeredTextString);
}
In the above test method, we create a StringBuilder where we pad it with spaces to add the required padding space and append our main content. Then, pad it again to add the rest of the padding spaces before printing the centered text.
在上述测试方法中,我们创建了一个StringBuilder,并在其中填充空格以添加所需的填充空间,然后附加主要内容。然后,在打印居中文本之前再次填充空格以添加其余的填充空间。
4. Using StringUtils
4.使用 StringUtils
One more easy way to center text is utilizing the center() method of the StringUtils class of Apache Commons Lang that was created especially for such purpose. Let’s practice the following example:
还有一种将文本居中的简单方法,那就是使用 Apache Commons Lang 的 StringUtils 类中的 center() 方法。让我们练习下面的示例:
@Test
public void givenTextAndTotalWidth_whenUsingStringUtilsCenterMethod_thenTextIsCentered() {
String text = "Centered Text";
int width = 15;
String centeredText = StringUtils.center(text, width);
assertEquals(" Centered Text ", centeredText);
}
In the above test method, we use the center() method of the StringUtils class that takes the text string and its total width. It then centers the text within the specified width.
在上述测试方法中,我们使用了 StringUtils 类中的 center() 方法,该方法接收 text 字符串及其总宽度。
5. Conclusion
5.结论
In conclusion, we can say that there are multiple ways to center text output in Java, such as String formatting, StringBuilder, and StringUtils from the Apache Commons Lang libraries.
总之,我们可以说,在 Java 中,有多种方法可以使文本输出居中,例如 Apache Commons Lang 库中的 String 格式化、StringBuilder 和 StringUtils 等。
Furthermore, these methods enhance the aesthetics of Java text-based applications, and users find it easy to interact with them.
此外,这些方法还增强了基于文本的 Java 应用程序的美感,用户也能轻松与之交互。
As always, the complete code samples for this article can be found over on GitHub.
一如既往,本文的完整代码示例可在 GitHub 上找到。