String.format Tutorial

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
时间: 2024-10-10 15:06:55

String.format Tutorial的相关文章

我的Android进阶之旅------>Java字符串格式化方法String.format()格式化float型时小数点变成逗号问题

今天接到一个波兰的客户说有个APP在英文状态下一切运行正常,但是当系统语言切换到波兰语言的时候,程序奔溃了.好吧,又是我来维护. 好吧,先把系统语言切换到波兰语,切换到波兰语的方法查看文章 我的Android进阶之旅------>Android[设置]-[语言和输入法]-[语言]列表中找到相应语言所对应的列表项 地址:http://blog.csdn.net/ouyang_peng/article/details/50209789 ================================

【java】Date与String之间的转换:java.text.SimpleDateFormat、public Date parse(String source) throws ParseException和public final String format(Date date)

1 package 日期日历类; 2 3 import java.text.ParseException; 4 import java.text.SimpleDateFormat; 5 import java.util.Date; 6 7 public class TestDate { 8 public static void main(String[] args) { 9 Date date=new java.util.Date(); 10 System.out.println(date);/

String.format()方法使用说明

JDK1.5开始String类中提供了一个非常有用的方法String.format(String format, Object ... args) 查看源码得知其实是调用了Java.util.Formatter.format(String, Object...)方法 [java] view plain copy print? public static String format(String format, Object ... args) { return new Formatter().f

String.Format数字格式化输出 {0:N2} {0:D2} {0:C2} (转)

String.Format数字格式化输出 {0:N2} {0:D2} {0:C2} (转) //格式为sring输出 // Label1.Text = string.Format("asdfadsf{0}adsfasdf",a); // Label2.Text = "asdfadsf"+a.ToString()+"adsfasdf"; // Label1.Text = string.Format("asdfadsf{0:C}adsfas

C#中string.format的格式和用法

String.Format 方法的几种定义: String.Format (String, Object) 将指定的 String 中的格式项替换为指定的 Object 实例的值的文本等效项. String.Format (String, Object[]) 将指定 String 中的格式项替换为指定数组中相应 Object 实例的值的文本等效项. String.Format (IFormatProvider, String, Object[]) 将指定 String 中的格式项替换为指定数组中

C#中的String.Format方法(转)

一.定义String.Format是将指定的 String类型的数据中的每个格式项替换为相应对象的值的文本等效项. 如: (1)string p1 = "Jackie";string p2 = "Aillo";Response.Write(String.Format("Hello {0}, I'm {1}", p1, p2));(2)Response.Write(String.Format("Hello {0}, I'm {1}&quo

c# string.Format用法总结

文章出处:http://www.cnblogs.com/7788/archive/2009/05/13/1455920.html 先举几个简单的应用案例: 1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格式化美元) string.Format("{0:C}",0.2) 结果为:¥0.20 (英文操作系统结果:$0.20) 默认格式化小数点后面保留两位小数,如果需要保留一位或者更多,可以指定位数string.Format("{0:C1}",23.

string.format、string.connect和+=运算 效率计算

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace StringFormatEfficiency { class Program { static void Main(string[] args) { string format = "my {0} is {1}"; string name = "name"; string zh

测试String.Format方法

今天想使用String.Format,和平时的用法不一样. 直接上代码: [Test] public void TestMethod6() { string A = "A"; string B = "B"; string C = "C"; string D = "{0}"; String str = String.Format(D, A, B, C); Assert.AreEqual(str, "A");