什么是“%1 $#”?是否在String.format(Java)中使用? [英] What does "%1$#" mean when used in String.format (Java)?

查看:431
本文介绍了什么是“%1 $#”?是否在String.format(Java)中使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

语言是Java。 %1 $#意思是...

  static String padright(String str,int num){
return String.format(%1 $#+ num +str,str);



$ b $ p $在Java API中, String.format() 用于这种方式:

  public static String format(String format,Object ... args )

所以我认为%1 $#是一个格式说明符。



%[flags] [width] [。precision] [argsize] typechar 是模板。




  • 1是一个标志?

  • $是宽度?

  • #是精确度吗?

  • num是argsize吗?

  • str是typechar?



是吗?

 %[argument_index $] [flags] [width] [。precision]转换
code>




可选的argument_index是
的十进制整数,表示
的位置
参数列表中的参数。第一个参数是
引用的1 $,第二个是
2 $等。

可选的标志是一组
字符的修改输出
格式。有效标志的集合取决于
的转换。

可选宽度是一个十进制整数,表示要写入
输出的最小
字符数。

可选的精度是
的非负十进制整数,通常是
,用于限制
字符的数量。特定行为
取决于转换。


所需的转换是一个
字符,指示参数
应该如何格式化。给定参数
的一组有效
转换取决于参数的数据类型。

%1 $ 指的是第一个替换。在这种情况下,字符串 str
是一个标志,表示结果应该使用一个依赖于转换的替代形式。

http://download.oracle.com/javase/1.5.0/docs/ api / java / util / Formatter.html


Language is Java. What does the %1$# mean in...

static String padright (String str, int num) {
   return String.format("%1$#" + num + "str", str);
}

In the Java API, String.format() is used in this way:

public static String format(String format, Object... args)

So I think %1$# is a format specifier.

%[flags][width][.precision][argsize]typechar is the template.

  • 1 is a flag?
  • $ is the width?
  • # is the precision?
  • num is the argsize?
  • "str" is the typechar?

Is that right?

解决方案

Template:

%[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 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.

%1$ refers to the first substitution. In this case the string str. # is flag which says the result should use a conversion-dependent alternate form.

http://download.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html

这篇关于什么是“%1 $#”?是否在String.format(Java)中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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