string.format用法
发布网友
发布时间:2022-04-27 01:29
我来回答
共6个回答
热心网友
时间:2022-04-18 05:14
参数
format
类型:System..::.String
复合格式字符串。
args
类型:array<System..::.Object>[]()[]
包含零个或多个要格式化的对象的 Object 数组。
返回值
类型:System..::.String
format 的一个副本,其中格式项已替换为 args 中相应 Object 实例的 String 等效项。
异常
异常 条件
ArgumentNullException format 或 args 为 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing)。
FormatException format 无效。
- 或 -
用于指示要格式化的参数的数字小于零,或者大于等于 args 数组的长度。
备注
此方法使用 .NET Framework 的复合格式设置功能将对象的值转换为其文本表示形式,并将该表示形式嵌入字符串中。.NET Framework 提供了广泛的格式设置支持,下面的格式设置主题中对此有更详细的描述。
有关 Format、AppendFormat 等方法以及 WriteLine 的某些重载所支持的复合格式设置功能的更多信息,请参见复合格式设置。
有关数值格式说明符的更多信息,请参见标准数字格式字符串和自定义数字格式字符串。
有关日期和时间格式说明符的更多信息,请参见标准日期和时间格式字符串和自定义日期和时间格式字符串。
有关枚举格式说明符的更多信息,请参见枚举格式字符串。
有关格式设置的更多信息,请参见为类型设置格式和格式设置概述。
format 参数由零个或多个文本序列与零个或多个索引占位符混合组成,其中索引占位符称为格式项,它们与此方法的参数列表中的对象相对应。格式设置过程将每个格式项替换为相应对象值的文本表示形式。
格式项的语法是 {索引[,对齐方式][:格式字符串]},它指定了一个强制索引、格式化文本的可选长度和对齐方式,以及格式说明符字符的可选字符串,其中格式说明符字符用于控制如何设置相应对象的值的格式。格式项的组成部分包括:
索引
从零开始的整数,指示对象列表中要格式化的元素。如果由索引指定的对象为 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing),则格式项将被空字符串 ("") 替换。
对齐方式
可选整数,指示包含格式化值的区域的最小宽度。如果格式化值的长度小于对齐方式,则用空格填充该区域。如果对齐方式为负,则格式化值将在该区域中左对齐;如果对齐方式为正,则格式化值将右对齐。如果没有指定对齐方式,则该区域的长度为格式化值的长度。如果指定对齐方式,则需要使用逗号。
格式字符串
可选的格式说明符字符串。如果没有指定格式字符串,并且对应的参数实现了 IFormattable 接口,则将 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing) 用作 IFormattable..::.ToString 格式字符串。因此,IFormattable..::.ToString 的所有实现都必须允许 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing) 作为格式字符串,并以 String 对象的形式返回对象表示形式的默认格式设置。如果指定格式字符串,则需要使用冒号。
必须使用前导大括号字符和尾部大括号字符,即“{”和“}”。若要在 format 中指定单个大括号字符,请指定两个前导大括号字符或尾部大括号字符;即“{{”或“}}”。
如果 format 的值为“Thank you for your purchase of {0:####} copies of Microsoft®.NET (Core Reference).”,并且 arg[0] 是值为 123 的 Int16,则返回值为:
“Thank you for your purchase of 123 copies of Microsoft®.NET (Core Reference).”
如果 format 的值为“Brad's dog has {0,-8:G} fleas.”,arg[0] 是值为 42 的 Int16(在此示例中,下划线表示填充空格),则返回值为:
“Brad's dog has 42______ fleas.”
示例
下面的代码示例演示数字、日期和枚举的标准格式设置说明符。
Visual Basic 复制代码
' This code example demonstrates the String.Format() method.
' This example uses the provider parameter to supply formatting
' information using the invariant culture.
Imports System.Globalization
Class Sample
Public Enum Color
Yellow = 1
Blue = 2
Green = 3
End Enum 'Color
Private Shared thisDate As DateTime = DateTime.Now
Public Shared Sub Main()
' Store the output of the String.Format method in a string.
Dim s As String = ""
Console.Clear()
' Format a negative integer or floating-point number in various ways.
Console.WriteLine("Standard Numeric Format Specifiers")
s = String.Format(CultureInfo.InvariantCulture, _
"(C) Currency: . . . . . . . . {0:C}" & vbCrLf & _
"(D) Decimal:. . . . . . . . . {0:D}" & vbCrLf & _
"(E) Scientific: . . . . . . . {1:E}" & vbCrLf & _
"(F) Fixed point:. . . . . . . {1:F}" & vbCrLf & _
"(G) General:. . . . . . . . . {0:G}" & vbCrLf & _
" (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _
"(N) Number: . . . . . . . . . {0:N}" & vbCrLf & _
"(P) Percent:. . . . . . . . . {1:P}" & vbCrLf & _
"(R) Round-trip: . . . . . . . {1:R}" & vbCrLf & _
"(X) Hexadecimal:. . . . . . . {0:X}" & vbCrLf, _
- 123, - 123.45F)
Console.WriteLine(s)
' Format the current date in various ways.
Console.WriteLine("Standard DateTime Format Specifiers")
s = String.Format(CultureInfo.InvariantCulture.DateTimeFormat, _
"(d) Short date: . . . . . . . {0:d}" & vbCrLf & _
"(D) Long date:. . . . . . . . {0:D}" & vbCrLf & _
"(t) Short time: . . . . . . . {0:t}" & vbCrLf & _
"(T) Long time:. . . . . . . . {0:T}" & vbCrLf & _
"(f) Full date/short time: . . {0:f}" & vbCrLf & _
"(F) Full date/long time:. . . {0:F}" & vbCrLf & _
"(g) General date/short time:. {0:g}" & vbCrLf & _
"(G) General date/long time: . {0:G}" & vbCrLf & _
" (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _
"(M) Month:. . . . . . . . . . {0:M}" & vbCrLf & _
"(R) RFC1123:. . . . . . . . . {0:R}" & vbCrLf & _
"(s) Sortable: . . . . . . . . {0:s}" & vbCrLf & _
"(u) Universal sortable: . . . {0:u} (invariant)" & vbCrLf & _
"(U) Universal sortable: . . . {0:U}" & vbCrLf & _
"(Y) Year: . . . . . . . . . . {0:Y}" & vbCrLf, _
thisDate)
Console.WriteLine(s)
' Format a Color enumeration value in various ways.
Console.WriteLine("Standard Enumeration Format Specifiers")
s = String.Format(CultureInfo.InvariantCulture, _
"(G) General:. . . . . . . . . {0:G}" & vbCrLf & _
" (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _
"(F) Flags:. . . . . . . . . . {0:F} (flags or integer)" & vbCrLf & _
"(D) Decimal number: . . . . . {0:D}" & vbCrLf & _
"(X) Hexadecimal:. . . . . . . {0:X}" & vbCrLf, _
Color.Green)
Console.WriteLine(s)
End Sub 'Main
End Class 'Sample
'
' This example displays the following output to the console:
'
' Standard Numeric Format Specifiers
' (C) Currency: . . . . . . . . (123.00)
' (D) Decimal:. . . . . . . . . -123
' (E) Scientific: . . . . . . . -1.234500E+002
' (F) Fixed point:. . . . . . . -123.45
' (G) General:. . . . . . . . . -123
' (default):. . . . . . . . -123 (default = 'G')
' (N) Number: . . . . . . . . . -123.00
' (P) Percent:. . . . . . . . . -12,345.00 %
' (R) Round-trip: . . . . . . . -123.45
' (X) Hexadecimal:. . . . . . . FFFFFF85
'
' Standard DateTime Format Specifiers
' (d) Short date: . . . . . . . 07/09/2007
' (D) Long date:. . . . . . . . Monday, 09 July 2007
' (t) Short time: . . . . . . . 13:42
' (T) Long time:. . . . . . . . 13:42:50
' (f) Full date/short time: . . Monday, 09 July 2007 13:42
' (F) Full date/long time:. . . Monday, 09 July 2007 13:42:50
' (g) General date/short time:. 07/09/2007 13:42
' (G) General date/long time: . 07/09/2007 13:42:50
' (default):. . . . . . . . 07/09/2007 13:42:50 (default = 'G')
' (M) Month:. . . . . . . . . . July 09
' (R) RFC1123:. . . . . . . . . Mon, 09 Jul 2007 13:42:50 GMT
' (s) Sortable: . . . . . . . . 2007-07-09T13:42:50
' (u) Universal sortable: . . . 2007-07-09 13:42:50Z (invariant)
' (U) Universal sortable: . . . Monday, 09 July 2007 20:42:50
' (Y) Year: . . . . . . . . . . 2007 July
'
' Standard Enumeration Format Specifiers
' (G) General:. . . . . . . . . Green
' (default):. . . . . . . . Green (default = 'G')
' (F) Flags:. . . . . . . . . . Green (flags or integer)
' (D) Decimal number: . . . . . 3
' (X) Hexadecimal:. . . . . . . 00000003
C# 复制代码
// This code example demonstrates the String.Format() method.
// Formatting for this example uses the "en-US" culture.
using System;
using System.Globalization;
class Sample
{
enum Color {Yellow = 1, Blue, Green};
static DateTime thisDate = DateTime.Now;
public static void Main()
{
// Store the output of the String.Format method in a string.
string s = "";
Console.Clear();
// Format a negative integer or floating-point number in various ways.
Console.WriteLine("Standard Numeric Format Specifiers");
s = String.Format(CultureInfo.InvariantCulture,
"(C) Currency: . . . . . . . . {0:C}\n" +
"(D) Decimal:. . . . . . . . . {0:D}\n" +
"(E) Scientific: . . . . . . . {1:E}\n" +
"(F) Fixed point:. . . . . . . {1:F}\n" +
"(G) General:. . . . . . . . . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(N) Number: . . . . . . . . . {0:N}\n" +
"(P) Percent:. . . . . . . . . {1:P}\n" +
"(R) Round-trip: . . . . . . . {1:R}\n" +
"(X) Hexadecimal:. . . . . . . {0:X}\n",
-123, -123.45f);
Console.WriteLine(s);
// Format the current date in various ways.
Console.WriteLine("Standard DateTime Format Specifiers");
s = String.Format(CultureInfo.InvariantCulture.DateTimeFormat,
"(d) Short date: . . . . . . . {0:d}\n" +
"(D) Long date:. . . . . . . . {0:D}\n" +
"(t) Short time: . . . . . . . {0:t}\n" +
"(T) Long time:. . . . . . . . {0:T}\n" +
"(f) Full date/short time: . . {0:f}\n" +
"(F) Full date/long time:. . . {0:F}\n" +
"(g) General date/short time:. {0:g}\n" +
"(G) General date/long time: . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(M) Month:. . . . . . . . . . {0:M}\n" +
"(R) RFC1123:. . . . . . . . . {0:R}\n" +
"(s) Sortable: . . . . . . . . {0:s}\n" +
"(u) Universal sortable: . . . {0:u} (invariant)\n" +
"(U) Universal sortable: . . . {0:U}\n" +
"(Y) Year: . . . . . . . . . . {0:Y}\n",
thisDate);
Console.WriteLine(s);
// Format a Color enumeration value in various ways.
Console.WriteLine("Standard Enumeration Format Specifiers");
s = String.Format(CultureInfo.InvariantCulture,
"(G) General:. . . . . . . . . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\n" +
"(D) Decimal number: . . . . . {0:D}\n" +
"(X) Hexadecimal:. . . . . . . {0:X}\n",
Color.Green);
Console.WriteLine(s);
}
}
/*
This example displays the following output to the console:
Standard Numeric Format Specifiers
(C) Currency: . . . . . . . . (123.00)
(D) Decimal:. . . . . . . . . -123
(E) Scientific: . . . . . . . -1.234500E+002
(F) Fixed point:. . . . . . . -123.45
(G) General:. . . . . . . . . -123
(default):. . . . . . . . -123 (default = 'G')
(N) Number: . . . . . . . . . -123.00
(P) Percent:. . . . . . . . . -12,345.00 %
(R) Round-trip: . . . . . . . -123.45
(X) Hexadecimal:. . . . . . . FFFFFF85
Standard DateTime Format Specifiers
(d) Short date: . . . . . . . 07/09/2007
(D) Long date:. . . . . . . . Monday, 09 July 2007
(t) Short time: . . . . . . . 13:48
(T) Long time:. . . . . . . . 13:48:05
(f) Full date/short time: . . Monday, 09 July 2007 13:48
(F) Full date/long time:. . . Monday, 09 July 2007 13:48:05
(g) General date/short time:. 07/09/2007 13:48
(G) General date/long time: . 07/09/2007 13:48:05
(default):. . . . . . . . 07/09/2007 13:48:05 (default = 'G')
(M) Month:. . . . . . . . . . July 09
(R) RFC1123:. . . . . . . . . Mon, 09 Jul 2007 13:48:05 GMT
(s) Sortable: . . . . . . . . 2007-07-09T13:48:05
(u) Universal sortable: . . . 2007-07-09 13:48:05Z (invariant)
(U) Universal sortable: . . . Monday, 09 July 2007 20:48:05
(Y) Year: . . . . . . . . . . 2007 July
Standard Enumeration Format Specifiers
(G) General:. . . . . . . . . Green
(default):. . . . . . . . Green (default = 'G')
(F) Flags:. . . . . . . . . . Green (flags or integer)
(D) Decimal number: . . . . . 3
(X) Hexadecimal:. . . . . . . 00000003
*/
热心网友
时间:2022-04-18 06:32
String.Format (String, Object) 将指定的 String 中的格式项替换为指定的 Object 实例的值的文本等效项。
String.Format (String, Object[]) 将指定 String 中的格式项替换为指定数组中相应 Object 实例的值的文本等效项。
String.Format (IFormatProvider, String, Object[]) 将指定 String 中的格式项替换为指定数组中相应 Object 实例的值的文本等效项。指定的参数提供区域性特定的格式设置信息。
String.Format (String, Object, Object) 将指定的 String 中的格式项替换为两个指定的 Object 实例的值的文本等效项。
String.Format (String, Object, Object, Object) 将指定的 String 中的格式项替换为三个指定的 Object 实例的值的文本等效项。
热心网友
时间:2022-04-18 08:07
string str=string.Format("字符串和参数{0} {1}……",value1,value2);
{0}对应逗号后第一个值 如 value1
其他类推
这是最常用的 返回指定格式的 字符串
热心网友
时间:2022-04-18 09:58
MessageBox.Show(string.Format("不存在用户{0},请重新输入!",
textBox2.Text)); 在format规定的字符串里,有{数字}这样的占位符,数字是几,就代表是后面的第几个参数。所以在这里是{0},代表把参数textBox2.Text放进字符串中的这个位置。就等同于 MessageBox.Show("不存在用户"
+ textBox2.Text
+
",请重新输入!"); 下面的sql也是一样到道理。将state的值替换到{0}的位置。 因为参数可以是个数组,所以才会有{0}{1}{2}……分别替换你的参数数组里第几个值。
热心网友
时间:2022-04-18 12:06
string.format(str,text,....)
str 是字符串,{0}。。是字符串中的参数的值
如:string.format("select * from aa where id={0} and name='{1}'",txtid.text,txtname.text);
热心网友
时间:2022-04-18 14:31
string.format(str,text,....)
string.format()的用法?
string.format是一个常用的字符串格式化方法,主要用于格式化字符串。该方法可以根据指定的格式,将变量值嵌入到字符串中,生成一个新的格式化字符串。这在编程中非常有用,特别是在需要生成特定格式的字符串输出时。详细解释:1. 基本用法:string.format允许你使用特定的格式占位符,如%s、%d、%f等,来...
string.format
string.format是用于格式化字符串的方法。解释:string.format的定义 string.format是一种在编程中常用的字符串格式化方法。它的主要作用是将数据按照指定的格式进行排版,生成特定格式的字符串。这种方法在多种编程语言中都有应用,例如C#、Java、Python等。string.format的功能 string.format功能十分强大,它允...
string.format()的用法?
在使用时,String.format() 方法有两种重载形式:format(String format, Object... args): 该方法根据指定的字符串格式和参数生成格式化字符串,会使用本地语言环境。 format(Locale locale, String format, Object... args): 这个版本允许你指定特定的语言环境来生成格式化字符串。总的来说,String.f...
string.format用法
String.format()字符串常规类型格式化的两种重载方式 format(String format, Object… args) 新字符串使用本地语言环境,制定字符串格式和参数生成格式化的新字符串。format(Locale locale, String format, Object… args) 使用指定的语言环境,制定字符串格式和参数生成格式化的字符串。
string.format()的用法?
String.format()用法 1、转换符 s: 字符串类型,如:"ljq"b: 布尔类型,如:true d: 整数类型(十进制),如:99 f: 浮点类型,如:99.99 : 百分比类型,如:% n: 换行符 2、常见日期时间格式化 c: 包括全部日期和时间信息 星期六 十月 27 14:21:20 CST 2007 F: "年-月-日"格式,...
String.format()的详细用法
String.format()是Java中常用的字符串格式化方法,它提供了两种主要的重载方式,以方便我们根据不同需求对字符串进行定制化格式化。在进行字符串格式化时,其中一种常见的是使用百分号(%)作为占位符,如%tx。在这里,'x'是一个日期转换符,用于处理日期格式。除此之外,String.format()还支持其他丰富的...
String.format
string.format()第一个参数为字符串格式,后面的参数可以任意多个,用于填充第一个参数中的格式控制符,最后返回完整的格式化后的字符串。 格式控制符以%开头,常用的有以下几种 %s -接受一个字符串并按照给定的参数格式化该字符串 %d -接受一个数字并将其转化为有符号的整数格式 %f -接受一个数字并将其转化为浮点...
String.format("%02d", year)是什么意思?
1、String.format("%02d", year)2、year格式化为至少2位十进制整数 譬如int year = 5; 结果为05 3、string.format 是格式化字符串 最大的好处 是 有多个参数的时候 只在内存中分布一个字符串如过用++的方式就会分布多个 例如:C# codestring sql="select " + colname + "from "+tablename...
聊聊String.format()方法
原因是当1个参数会在模板字符串中多次使用时,我们可以使用 数字$ 的方式来对指定位置的参数进行替换。比如说 1$ 表示的就是这里将使用第1个参数进行替换。 同时,还需要注意,我们的索引占位符 数字$ 是需要写在%之后的,否则会识别错误。至此,对于String.format方法我们就介绍到这里。
JAVA String.format方法使用介绍
在JDK1.5中,String类增加了一个非常有用的静态函数format(String format, Objece... argues),可以将各类数据格式化为字符串并输出。其中format参数指定了输出的格式,是最复杂也是最难掌握的一点,而argues则是一系列等 待被格式化的对象。该函数对c语言中printf函数的用法进行了一定的模仿,因此有c...