日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

记录对String.format(Formatter().format())方法的总结

發(fā)布時(shí)間:2024/7/5 编程问答 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 记录对String.format(Formatter().format())方法的总结 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

String.format其實(shí)是調(diào)用的Formatter.format:

public static String format(String format, Object... args) {return new Formatter().format(format, args).toString();}
第一個(gè)參數(shù)是格式化字符串,第二個(gè)參數(shù)是可變的被格式化的參數(shù)。

我們主要看的是格式化參數(shù)的說(shuō)明:

  • The format specifiers for general, character, and numeric types have the following syntax: %[argument_index$][flags][width][.precision]conversion

這個(gè)參數(shù)是用來(lái)格式化通用的、字符串、數(shù)字類(lèi)型,主要說(shuō)一下每個(gè)參數(shù)的作用:
% 用于表示這后面將是一個(gè)格式化數(shù)據(jù)的模板,
argument_index$ 表示要作用在第幾個(gè)參數(shù)上,注意一定要加上'$',
flags 以下是flag的對(duì)照表,對(duì)不同的類(lèi)型有不同的作用:
  • The following table summarizes the supported flags. y means the flag is supported for the indicated argument types.

    FlagGeneralCharacterIntegralFloating PointDate/TimeDescription
    '-'yyyyyThe result will be left-justified.
    '#'y1-y3y-The result should use a conversion-dependent alternate form
    '+'--y4y-The result will always include a sign
    '??'--y4y-The result will include a leading space for positive values
    '0'--yy-The result will be zero-padded
    ','--y2y5-The result will include locale-specific grouping separators
    '('--y4y5-The result will enclose negative numbers in parentheses

    1 Depends on the definition of Formattable.

    2 For 'd' conversion only.

    3 For 'o', 'x', and 'X' conversions only.

    4 For 'd', 'o', 'x', and 'X' conversions applied to BigInteger or 'd' applied to byte, Byte, short, Short, int and Integer, long, and Long.

    5 For 'e', 'E', 'f', 'g', and'G' conversions only.

    Any characters not explicitly defined as flags are illegal and are reserved for future extensions.?

  • Width

    The width is the minimum number of characters to be written to the output. For the line separator conversion, width is not applicable; if it is provided, an exception will be thrown.

    Precision

    For general argument types, the precision is the maximum number of characters to be written to the output.

    For the floating-point conversions 'a', 'A', 'e','E', and 'f' the precision is the number of digits after the radix point. If the conversion is'g' or 'G', then the precision is the total number of digits in the resulting magnitude after rounding.

    For character, integral, and date/time argument types and the percent and line separator conversions, the precision is not applicable; if a precision is provided, an exception will be thrown.

    Argument Index

    The 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.

    Another way to reference arguments by position is to use the '<' ('\u003c') flag, which causes the argument for the previous format specifier to be re-used. For example, the following two statements would produce identical strings:

    Calendar c = ...;String s1 = String.format("Duke's Birthday: %1$tm %1$te,%1$tY", c);String s2 = String.format("Duke's Birthday: %1$tm %<te,%<tY", c);
最重要的是conversion: ConversionArgument CategoryDescription
'b', 'B'generalIf the argument arg is null, then the result is "false". Ifarg is a boolean or Boolean, then the result is the string returned by String.valueOf(arg). Otherwise, the result is "true".
'h', 'H'generalIf the argument arg is null, then the result is "null". Otherwise, the result is obtained by invokingInteger.toHexString(arg.hashCode()).
's', 'S'generalIf the argument arg is null, then the result is "null". Ifarg implements Formattable, then arg.formatTo is invoked. Otherwise, the result is obtained by invokingarg.toString().
'c', 'C'characterThe result is a Unicode character
'd'integralThe result is formatted as a decimal integer
'o'integralThe result is formatted as an octal integer
'x', 'X'integralThe result is formatted as a hexadecimal integer
'e', 'E'floating pointThe result is formatted as a decimal number in computerized scientific notation
'f'floating pointThe result is formatted as a decimal number
'g', 'G'floating pointThe result is formatted using computerized scientific notation or decimal format, depending on the precision and the value after rounding.
'a', 'A'floating pointThe result is formatted as a hexadecimal floating-point number with a significand and an exponent. This conversion isnot supported for the BigDecimal type despite the latter's being in thefloating point argument category.
't', 'T'date/timePrefix for date and time conversion characters. See Date/Time Conversions.
'%'percentThe result is a literal '%' ('\u0025')
'n'line separatorThe result is the platform-specific line separator?

示例代碼:

package main;public class StringFormat {public static void main(String[] args) {// TODO Auto-generated method stubString format = "Haha.%1$(g%2$s%3$s";Object obj = 100f, object1 = "fdsafds", object2 = new StringFormat();String format2 = String.format(format, obj, object1, object2);System.out.println(format2);// String Format總結(jié)// %[argument_index$][flags][width][.precision]conversion 標(biāo)準(zhǔn)表達(dá)式}}

運(yùn)行結(jié)果:

Haha.100.000fdsafdsmain.StringFormat@6d6f6e28

總結(jié)

以上是生活随笔為你收集整理的记录对String.format(Formatter().format())方法的总结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。