1
2
3
4
var s1 = string.Format("{0,-10}", 8);
var s2 = string.Format("{0,10}", 8);
var s3 = string.Format("{0,20:yyyy-MM-dd}", DateTime.Today);
var s4 = string.Format("4G 内存便宜了{0,12:C2},我打算买{1,4}条", 145, 2);

大括号中,索引后 分号前,有一个逗号和一个整数(减号表示负数)。

string.Format("{0,-10}", 8) 等同于 string.Format("{0}", 8).PadRight(10);

string.Format("{0,10}", 8) 等同于 string.Format("{0}", 8).PadLeft(10)。

String.Format 方法 format 参数由零或多个文本序列与零或多个索引占位符混合组成,其中索引占位符称为格式项,对应于与此方法的参数列表中的对象。 格式设置过程将每个格式项替换为相应对象值的字符串表示形式。

格式项的语法如下:

{index[,length][:formatString]}

优点:

,length 其实把 String.PadLeft 和 String.PadRight 的功能融合在的 String.Format 方法中,简化了代码的编写。

如果没有这种格式,代码写起来麻烦多了:

1
2
3
var s5 = string.Format("4G 内存便宜了{0},我打算买{1}条",
145.ToString("C2").PadLeft(12),
2.ToString().PadLeft(4));

缺点:

看起来不直观,复杂,如文首代码中的 “我打算买{1,4}条”。

实际使用少,没用过的看了很纳闷。

-------------------

转载:http://www.cnblogs.com/ldp615/archive/2011/08/03/string-format-with-length.html

这种写法用过没:string.Format("{0,-10}", 8)的更多相关文章

  1. try { var mergeFilePath = string.Format("{0}mergepdf.pdf", tempDownDir); PDFPrintHelper.MergePDFFile(pdfList, mergeFi

    winform 按顺序连续打印多个PDF文件   关于PDF打印的问题,前面有篇文章(点这里查看)也叙述过,今天来谈谈另外一种方法 其实方法很简单,因为需要把多个PDF文档按顺序连续打印,为此我们为什 ...

  2. String.format("%0"+length+"d", arr)中的%0和"d"分别代表什么

    public static void main(String[] args) { int a = 8; String s = String.format("%04d", a); S ...

  3. C# string.Format("{0:C3}", 2)

  4. 工作总结 @{var sas = String.Format("{0:yyyy-MM-dd}", Model.DemandTime.GetValueOrDefault());}

      可空DateTime类型 无法Tostring 或者 格式化时间格式   需要转换 为DateTime Nullable<T>.GetValueOrDefault() 方法 返回对应的 ...

  5. string.Format出现异常"输入的字符串格式有误"的解决方法

    string.Format出现异常"输入的字符串格式有误"的解决方法 今天在做项目时,碰到一个很奇怪的问题,我使用string.Format居然报“输入的字符串格式有误”的错误,我 ...

  6. C# String.Format大全

    C# String.Format大全 ? ? ? 十进制的数字 ? ? string.Format("{0:D3}",23) 023 格式化十进制的数字 string.Format ...

  7. C# String.Format的格式限定符与Format方法将多个对象格式化一个字符串原理

    Format方法将多个对象格式化成一个字符串Format方法解析格式字符串的原理: (1).格式字符串中的{0}会被替换成格式字符串之后的第一个参数,以此类推 (2).Format方法解析格式字符串时 ...

  8. String Format for Double [C#]

    转载:http://www.csharp-examples.net/string-format-double/ he following examples show how to format flo ...

  9. C#:String.Format数字格式化输出 {0:N2} {0:D2} {0:C2}等等

    int a = 12345678; //格式为sring输出//   Label1.Text = string.Format("asdfadsf{0}adsfasdf",a); / ...

随机推荐

  1. tlplayer for android V2.7(支持变速不变调) 2014-07-20更新

    2014-07-20新版本已经修复部分视频倾斜问题,已经更新到服务器,需要的朋友自行下载. 此版本修正了倾斜问题,增加水印等功能.可以动态添加水印. tlplayer for android V2.6 ...

  2. Java之关键字static和final的使用

    static 在Java中声明属性.方法时,可使用关键字static来修饰. 1.static变量       按照是否静态的对类成员变量进行分类可分两种:一种是被static修饰的变量,叫静态变量或 ...

  3. 使用C#在word中插入页眉页脚

    //插入页脚 public void InsertFooter(string footer) { if (ActiveWindow.ActivePane.View.Type == WdViewType ...

  4. 【转】 Java虚拟机内存的堆区(heap),栈区(stack)和静态区(static/method)

    JAVA的JVM的内存可分为3个区:堆(heap).栈(stack)和方法区(method) 堆区:1.存储的全部是对象,每个对象都包含一个与之对应的class的信息.(class的目的是得到操作指令 ...

  5. C# Word 类库的深入理解

    using System;using System.Collections.Generic;using System.Text;using Microsoft.Office.Interop.Word; ...

  6. Android中TextView输入字数统计和限制

    在Android开发应用的时候,文本编辑框中最多输入140个字,经常会显示还剩多少字以限制用户输入的字数, EditText content;//定义一个文本输入框 TextView hasnum;/ ...

  7. North North West

    North North West Time Limit: 10000ms, Special Time Limit:25000ms, Memory Limit:65536KB Total submit ...

  8. 文件操作总结:关于文本和二进制流(typeText&typeBinary)

    本人能力.精力有限,所言所感都基于自身的实践和有限的阅读.查阅,如有错误,欢迎拍砖,敬请赐教——博客园:钱智慧. 总结: CFile,其自身是不提供缓冲区的(?但CFile又有一个Flush,这一点目 ...

  9. poj2192(搜索)

    这个题目对于两个字符串A,B是否可以通过规则生成C. import java.util.Scanner; public class Main { public static void main(Str ...

  10. Java笔记(二十七)……IO流中 File文件对象与Properties类

    File类 用来将文件或目录封装成对象 方便对文件或目录信息进行处理 File对象可以作为参数传递给流进行操作 File类常用方法 创建 booleancreateNewFile():创建新文件,如果 ...