理解Java中的$格式字符串 [英] Understanding the $ in Java's format strings

查看:131
本文介绍了理解Java中的$格式字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 StringBuilder sb = new StringBuilder();
 // Send all output to the Appendable object sb
 Formatter formatter = new Formatter(sb, Locale.US);

 // Explicit argument indices may be used to re-order output.
 formatter.format("%4$2s %3$2s %2$2s %1$2s", "a", "b", "c", "d")
 // -> " d  c  b  a"

在这种情况下,为什么2附加到$?

In this case, why is a 2 appended to $?

推荐答案

2 与<$ c $无关c> $ :


  •     ;&NBSP; = 格式字符串的开头

  • 4 $   = 第四个参数('d')

  • 2     = 宽度为2(右对齐)

  • s     = 字符串类型

  • %     =   Start of format string
  • 4$   =   Fourth argument ('d')
  • 2     =   width of two (right-aligned)
  • s     =   type of String

这篇关于理解Java中的$格式字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆