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

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

%[argument_index$][flags][width][.precision]conversion
  • argument_index is a decimal integer indicating the position of the argument in the argument list. It starts from "1$".
  • flags is a set of  characters that modify the output format.
Flag General Character Integer Floating Point Date/Time Description
'-' Y Y Y Y Y The result will be left-justified
'#' Y - Y Y - The result should use a conversion-dependent alternate form
'+' - - Y Y - The result will always include a sign
' '  - - Y Y - The result will include a leading space for positive values
'0' - - Y Y - The result will be zero-padded 
',' - - Y Y - The result will include locale-specific grouping separators 
'(' - - Y Y - The result will enclose negative numbers in parentheses 
  • width is a non-negative decimal interger indicating the minimum number of the number of characters to be written to the output.
  • For the floating-point conversions 'e''E', and 'f' the precision is the number of digits after the decimal separator. If the conversion is 'g' or 'G', then the precision is the total number of digits in the resulting magnitude after rounding. If the conversion is 'a' or 'A', then the precision must not be specified.
  • conversion are divided into the following categories:

    General, Character, Numeric(Integer, Floating Point), Date/Time, Percent, Line Separator

Conversion Argument Category Description
'b''B' general If the argument arg is null, then the result is "false". If arg is a boolean or Boolean, then the result is the string returned by String.valueOf(arg). Otherwise, the result is "true".
'h''H' general If the argument arg is null, then the result is "null". Otherwise, the result is obtained by invoking Integer.toHexString(arg.hashCode()).
's''S' general If the argument arg is null, then the result is "null". If arg implements Formattable, then arg.formatTo is invoked. Otherwise, the result is obtained by invokingarg.toString().
'c''C' character The result is a Unicode character
'd' integral The result is formatted as a decimal integer
'o' integral The result is formatted as an octal integer
'x''X' integral The result is formatted as a hexadecimal integer
'e''E' floating point The result is formatted as a decimal number in computerized scientific notation
'f' floating point The result is formatted as a decimal number
'g''G' floating point The result is formatted using computerized scientific notation or decimal format, depending on the precision and the value after rounding.
'a''A' floating point The result is formatted as a hexadecimal floating-point number with a significand and an exponent
't''T' date/time Prefix for date and time conversion characters. See Date/Time Conversions.
'%' percent The result is a literal '%' ('\u0025')
'n' line separator The result is the platform-specific line separator

String.format Tutorial的更多相关文章

  1. c# 字符串连接使用“+”和string.format格式化两种方式

    参考文章:http://www.liangshunet.com/ca/201303/218815742.htm 字符串之间的连接常用的两种是:“+”连接.string.format格式化连接.Stri ...

  2. 【转】string.Format对C#字符串格式化

    转自:http://blog.csdn.net/samsone/article/details/7556781 1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格式化美元) str ...

  3. C#中string.format用法详解

    C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...

  4. string.Format格式化用法详解

    1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格式化美元) string.Format("{0:C}",0.2) 结果为:¥0.20 (英文操作系统结果:$0 ...

  5. Js实现string.format

    经常需要动态拼接html字符串,想到用类似于.net的string.format函数比较好,于是找了下,stackoverflow的代码: if (!String.prototype.format) ...

  6. String.Format用法

    http://blog.csdn.net/yohop/article/details/2534907 1.作为参数   名称 说明   Format(String, Object) 将指定的 Stri ...

  7. String.Format 格式说明

    C#格式化数值结果表 字符 说明 示例 输出 C 货币 string.Format("{0:C3}", 2) $2.000 D 十进制 string.Format("{0 ...

  8. C# String.Format格式化json字符串中包含"{" "}"报错问题

    json.Append(String.Format("{\"total\":{0},\"row\":{1}}", lineCount, st ...

  9. JAVA字符串格式化-String.format()的使用

    String类的format()方法用于创建格式化的字符串以及连接多个字符串对象.熟悉C语言的同学应该记得C语言的sprintf()方法,两者有类似之处.format()方法有两种重载形式. form ...

随机推荐

  1. 16 Socket通信(简单例子)

      服务端代码: import java.io.*; import java.net.ServerSocket; import java.net.Socket; import java.util.Da ...

  2. tableview 里面的 必须配套使用的方法

    1: 如果不配套的先给出  footer的高度,那么  即时有view 可能也显示不出来 - (CGFloat)tableView:(UITableView *)tableView heightFor ...

  3. hdu 2473 Junk-Mail Filter

    http://acm.hdu.edu.cn/showproblem.php?pid=2473 并查集设置虚拟父节点. #include <cstdio> #include <cstr ...

  4. 通过实现Comparable接口结合TreeSet来对对象自动排序

    经过会遇到这样的情况,对于某个对象数组或者链表要按照一定的规则进行排序,那么我们该怎么做呢? 如遇到这样的需求: 1.需求1 对于学生对象按照年龄进行排序,年龄小的排在前面. 单单看到这样的需求,实现 ...

  5. unix c 09

    IPC - 进程间通信   文件/信号/管道/共享内存/消息队列/信号量集/网络   XSI IPC (共享内存.消息队列和信号量集)   使用方式 非常的类似. 共享内存的使用步骤:     1 生 ...

  6. Word Pattern II 解答

    Question Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...

  7. Word Break II 解答

    Question Given a string s and a dictionary of words dict, add spaces in s to construct a sentence wh ...

  8. HDU1698 Just a Hook (区间更新)

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. Android应用开发学习之状态栏通知

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 状态栏通知涉及到两个类,一是Notification,它代表一个通知:另一个是NotificationManager ...

  10. extjs两个tbar问题

      版本:extjs3.4   接触过extjs的同志们都知道每个panel都有一个tbar(top bar 上面工具栏) ,bbar(bottom bar 底部工具栏)      大家做查询页面,一 ...