复合格式字符串和对象列表将用作支持复合格式设置功能的方法的参数。复合格式字符串由零个或多个固定文本段与一个或多个格式项混和组成。固定文本是所选择的任何字符串,并且每个格式项对应于列表中的一个对象或装箱的结构。复合格式设置功能返回新的结果字符串,其中每个格式项都被列表中相应对象的字符串表示形式取代。

可考虑使用以下 Format 代码段。

string name = "Fred";

String.Format("Name = {0}, hours = {1:hh}", name, DateTime.Now);

固定文本为“Name = ”和“, hours = ”。格式项为“{0}”和“{1:hh}”,前者的索引为 0,对应于对象 myName,后者的索引为 1,对应于对象 DateTime.Now。

格式项语法

每个格式项都采用下面的形式并包含以下组件:

{index[,alignment][:formatString]}" xml:space="preserve" id="mt24">{ index[,alignment][:formatString]}

必须使用成对的大括号(“{”和“}”)。

索引组件:

强制“索引”组件(也叫参数说明符)是一个从 0 开始的数字,可标识对象列表中对应的项。也就是说,参数说明符为 0 的格式项列表中的第一个对象,参数说明符为 1 的格式项列表中的第二个对象,依次类推。下面的示例包括五个参数说明符,用于表示小于 10 的质数:

string primes;
primes = String.Format("Prime numbers less than 10: {0}, {1}, {2}, {3}, {4}",  1, 2, 3, 5, 7 );
Console.WriteLine(primes);
// The example displays the following output:
// Prime numbers less than 10: 1, 2, 3, 5, 7

通过指定相同的参数说明符,多个格式项可以引用对象列表中的同一个元素。例如,通过指定诸如“0x{0:X} {0:E} {0:N}”的复合格式字符串,可以将同一个数值设置为十六进制、科学记数法和数字格式,如下面的示例所示。

string multiple = String.Format("0x{0:X} {0:E} {0:N}", Int64.MaxValue);
Console.WriteLine(multiple);
// The example displays the following output:
//      0x7FFFFFFFFFFFFFFF 9.223372E+018 9,223,372,036,854,775,807.00
 

每个格式项都可以引用列表中的任一对象。例如,如果有三个对象,则可以通过指定类似于“{1} {0} {2}”的复合格式字符串来设置第二、第一和第三个对象的格式。格式项未引用的对象会被忽略。如果参数说明符指定了超出对象列表范围的项,将引发运行时 FormatException。

对齐组件:

可选的“对齐”组件是一个带符号的整数,指示首选的设置了格式的字段宽度。alignment is less than the length of the formatted string, alignment is ignored and the length of the formatted string is used as the field width." xml:space="preserve" id="mt40">如果“对齐”值小于设置了格式的字符串的长度,“对齐”会被忽略,并且使用设置了格式的字符串的长度作为字段宽度。alignment is positive and left-aligned if alignment is negative." xml:space="preserve" id="mt41">如果“对齐”为正数,字段中设置了格式的数据为右对齐;如果“对齐”为负数,字段中的设置了格式的数据为左对齐。如果需要填充,则使用空白。alignment is specified." xml:space="preserve" id="mt43">如果指定 alignment,就需要使用逗号。

下面的示例定义两个数组,一个包含雇员的姓名,另一个则包含雇员在两周内的工作小时数。复合格式字符串使 20 字符字段中的姓名左对齐,使 5 字符字段中的工作小时数右对齐。请注意“N1”标准格式字符串还用于设置带有小数位的小时数格式。

using System;
public class Example
{
   public static void Main()
   {
      string[] names = { "Adam", "Bridgette", "Carla", "Daniel",
                         "Ebenezer", "Francine", "George" };
      decimal[] hours = { 40, 6.667m, 40.39m, 82, 40.333m, 80,
                                 16.75m };
      Console.WriteLine("{0,-20} {1,5}\n", "Name", "Hours");
      for (int ctr = 0; ctr < names.Length; ctr++)
         Console.WriteLine("{0,-20} {1,5:N1}", names[ctr], hours[ctr]);
 
   }
}
// The example displays the following output:
//       Name                 Hours
//
//       Adam                  40.0
//       Bridgette              6.7
//       Carla                 40.4
//       Daniel                82.0
//       Ebenezer              40.3
//       Francine              80.0
//       George                16.8

格式字符串组件

可选的“格式字符串”组件是适合正在设置格式的对象类型的格式字符串。DateTime object, or an enumeration format string if the corresponding object is an enumeration value." xml:space="preserve" id="mt50">如果相应的对象是数值,则指定标准或自定义的数字格式字符串;如果相应的对象是 DateTime 对象,则指定标准或自定义的日期和时间格式字符串;或者,如果相应的对象是枚举值,则指定枚举格式字符串。formatString is not specified, the general ("G") format specifier for a numeric, date and time, or enumeration type is used." xml:space="preserve" id="mt51">如果不指定 formatString,则对数字、日期和时间或者枚举类型使用常规(“G”)格式说明符。formatString is specified." xml:space="preserve" id="mt52">如果指定 formatString,则需要使用冒号。

下表列出了 .NET Framework 类库中支持预定义的格式字符串集的类型或类型的类别,并提供指向列出了支持的格式字符串的主题的链接。请注意,字符串格式化是一个可扩展的机制,可使用该机制定义所有现有类型的新的格式字符串,并定义受应用程序定义的类型支持的格式字符串集。IFormattable and ICustomFormatter interface topics." xml:space="preserve" id="mt55">有关详细信息,请参阅 IFormattable 和 ICustomFormatter 接口主题。

String.Format(string, arg0)中sring格式的更多相关文章

  1. string.format格式化字符串中转义大括号“{}”

    今天,用Java读取配置文件占位符,使用String.Format(string format,object arg0)方法.以前只知“{0}”为索引占位符(即格式项),与参数列表中的第一个对象相对应 ...

  2. 再探Java基础——String.format(String format, Object… args)的使用

    最近看到类似这样的一些代码:String.format("参数%s不能为空", "birthday"); 以前还没用过这功能不知咐意思,后研究了一下,详细讲解如 ...

  3. String.format(String format, Object... args)方法详解

    很多次见到同事使用这个方法,同时看到https://blog.csdn.net/qq_27298687/article/details/68921934这位仁兄写的非常仔细,我也记录一下,好加深印象. ...

  4. String.format(String format,Object... args)的用法

    String.format(String format, Object... args)方法详解 以前也看到过很多次这个用法,一直记不牢靠,今天整理一下.   我仅仅举几个例子稍做说明: String ...

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

    String.Format 方法的几种定义: String.Format (String, Object) 将指定的 String 中的格式项替换为指定的 Object 实例的值的文本等效项. Str ...

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

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

  7. string.Format字符串格式说明

    先举几个简单的应用案例: 1.格式化货币(跟系统的环境有关,中文系统默认格式化人民币,英文系统格式化美元) string.Format("{0:C}",0.2) 结果为:¥0.20 ...

  8. java中的String.format使用

         format(String  format, Objece...  argues)函数相当于C语言中的printf函数,但是相对来说更灵活.      和C中的printf函数差不多,在fo ...

  9. C#中string.Format 用法详解

    这篇文章主要介绍了C#中string.format用法,以实例形式较为详细的讲述了string.format格式化的各种用法,非常具有实用价值,需要的朋友可以参考下 本文实例总结了C#中string. ...

随机推荐

  1. 《JS权威指南学习总结--第8章 函数知识》

    内容要点: 1.函数表达式定义后立即调用: var tensquared = ( function(x){ return x*x ;}(10) ); 2.嵌套函数:       在JS里,函数可以嵌套 ...

  2. thinkphp u 方法

    public function test(){ $this->display();echo U('Index/test',array('id'=>1),false,'localhost') ...

  3. 【Sort】希尔排序

    希尔排序(ShellSort),缩小增量排序,使用希尔增量时最坏运行时间O(n^2),不同的增量会对运行时间产生显著影响. void shellsort(int *nums,int n) { int ...

  4. table 中的td 字段超长,超过部分用....表示

    #contentTable{ table-layout:fixed;}.contentShort{ text-overflow: ellipsis; overflow: hidden; white-s ...

  5. 向openwrt 源码添加ap143支持

    借鉴地址:http://www.pppei.net/blog/post/536 1.向文件 \target\linux\ar71xx\generic\profiles\atheros.mk 中添加ap ...

  6. LeetCode OJ 45. Jump Game II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  7. 洛谷-哥德巴赫猜想(升级版)-BOSS战-入门综合练习1

    题目背景 Background 1742年6月7日哥德巴赫写信给当时的大数学家欧拉,正式提出了以下的猜想:任何一个大于9的奇数都可以表示成3个质数之和.质数是指除了1和本身之外没有其他约数的数,如2和 ...

  8. swift中JSon数据的处理

    import UIKit class MainTabBarViewController: UITabBarController { override func viewDidLoad() { supe ...

  9. Spring的Resource

    通过Spring Resource接口获取资源(取自http://haohaoxuexi.iteye.com/blog/2016305)目录1 Resource简介2 通过ResourceLoader ...

  10. sql语句的group by与having子句

    准备数据: DROP TABLE IF EXISTS `t_player`; CREATE TABLE `t_player` ( `player_id` int(11) NOT NULL AUTO_I ...