java - 需要有关 String.format 的一些帮助

标签 java string formatting

我正在尝试找到有关在 java 中格式化字符串的完整教程。

我需要创建收据,如下所示:

       HEADER IN MIDDLE
''''''''''''''''''''''''''''''
Item1                   Price
Item2 x 5               Price
Item3 that has a very
long name....           Price
''''''''''''''''''''''''''''''
Netprice:                 xxx
Grossprice:               xxx
VAT:                      xxx
Shipping cost:            xxx
Total:                    xxx
''''''''''''''''''''''''''''''
      FOOTER IN MIDDLE

最佳答案

传递给 string.format 的格式记录如下:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html#syntax

来自页面:

The format specifiers for general, character, and numeric types have the following syntax:

   %[argument_index$][flags][width][.precision]conversion

The optional argument_index is a decimal integer indicating the position of the argument in the argument list. The first argument is referenced by "1$", the second by "2$", etc.

The optional flags is a set of characters that modify the output format. The set of valid flags depends on the conversion.

The optional width is a non-negative decimal integer indicating the minimum number of characters to be written to the output.

The optional precision is a non-negative decimal integer usually used to restrict the number of characters. The specific behavior depends on the conversion.

The required conversion is a character indicating how the argument should be formatted. The set of valid conversions for a given argument depends on the argument's data type.

关于java - 需要有关 String.format 的一些帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1588382/

相关文章:

Python:比较两个字符串保留一端的差异

SQL Server 将 Varchar 转换为日期时间

vba - protected 工作表允许通过复制和粘贴编辑单元格格式

excel - 在 Excel 图表上应用格式

java - 是否在任何地方指定匿名类的数量从 1 开始?

string - Powershell字符串分割差异

java - 线程 JFrame 需要更新 JTextPane

string - 字符串中的字符差异是否有正式定义?如果有,如何计算?

java - 当我们调用 LockSupport.unpark() 时线程将处于哪种状态

java - 获取ListView的X位置处的View