如果在String.format中多次使用相同的参数怎么办? [英] What about if you've got the same parameter multiple times in String.format?

查看:264
本文介绍了如果在String.format中多次使用相同的参数怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

String hello = "Hello";

String.format("%s %s %s %s %s %s", hello, hello, hello, hello, hello, hello);

hello hello hello hello hello hello 

hello变量是否需要在格式化方法的调用中重复多次,或者是否有一个简写版本,允许您指定一次应用于所有%s 标记的参数?

Does the hello variable need to be repeated multiple times in the call to the format method or is there a shorthand version that lets you specify the argument once to be applied to all of the %s tokens?

推荐答案

来自文档



  • 格式通用,字符和数字类型的说明符具有以下语法:

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

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

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

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.



String.format("%1$s %1$s %1$s %1$s %1$s %1$s", hello);

这篇关于如果在String.format中多次使用相同的参数怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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