本文介紹了在 String.format() 中選擇參數(shù)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
限時送ChatGPT賬號..
在 C#
中,您可以使用 para 2: {2}
指定用于格式化字符串的參數(shù).這允許在任意位置多次使用參數(shù).
In C#
you can specify which parameter is used for a formatted string with para 2: {2}
. This allows for using parameters in arbitrary places and multiple times.
有沒有辦法用標(biāo)準(zhǔn) java 做到這一點?
Is there a way to do this with standard java?
推薦答案
是的.您可以定義參數(shù)的索引,請參閱 Argument Index 部分.html" rel="noreferrer">API.
Yes. You can define the argument's index, see the Argument Index section of the API.
例如:
// ┌ argument 3 (1-indexed)
// | ┌ type of String
// | | ┌ argument 2
// | | | ┌ type of decimal integer
// | | | | ┌ argument 1
// | | | | | ┌ type of decimal number (float)
// | | | | | |
System.out.printf("%3$s %2$d %1$f", 1.5f, 42, "foo");
輸出
foo 42 1.500000
注意
以下成語都有相同的格式定義:
The following idioms all share the same format definitions:
String#format
PrintStream#printf
Formatter#format
這篇關(guān)于在 String.format() 中選擇參數(shù)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!