String.Format方法是我们在.Net应用开发时经常使用到的,它的灵活使用有时能够达到事半功倍的效果,下面我们就借用MSDN上的一个示例来向大家展示String.Format的各种用法。

该示例展示了Numeric、DateTime和Enumeration标准格式的使用,另外,我们也可以根据需要自定义我们需要的格式。

// This code example demonstrates the String.Format() method.

using System;
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 = "";

        // Format a negative integer or floating-point number in various ways.
        Console.WriteLine("Standard Numeric Format Specifiers");
        s = String.Format(
            "(C) Currency: . . . . . . . . {0:C} " +
            "(D) Decimal:. . . . . . . . . {0:D} " +
            "(E) Scientific: . . . . . . . {1:E} " +
            "(F) Fixed point:. . . . . . . {1:F} " +
            "(G) General:. . . . . . . . . {0:G} " +
            "    (default):. . . . . . . . {0} (default = 'G') " +
            "(N) Number: . . . . . . . . . {0:N} " +
            "(P) Percent:. . . . . . . . . {1:P} " +
            "(R) Round-trip: . . . . . . . {1:R} " +
            "(X) Hexadecimal:. . . . . . . {0:X} ",
            -123, -123.45f); 
        Console.WriteLine(s);

        // Format the current date in various ways.
        Console.WriteLine("Standard DateTime Format Specifiers");
        s = String.Format(
            "(d) Short date: . . . . . . . {0:d} " +
            "(D) Long date:. . . . . . . . {0:D} " +
            "(t) Short time: . . . . . . . {0:t} " +
            "(T) Long time:. . . . . . . . {0:T} " +
            "(f) Full date/short time: . . {0:f} " +
            "(F) Full date/long time:. . . {0:F} " +
            "(g) General date/short time:. {0:g} " +
            "(G) General date/long time: . {0:G} " +
            "    (default):. . . . . . . . {0} (default = 'G') " +
            "(M) Month:. . . . . . . . . . {0:M} " +
            "(R) RFC1123:. . . . . . . . . {0:R} " +
            "(s) Sortable: . . . . . . . . {0:s} " +
            "(u) Universal sortable: . . . {0:u} (invariant) " +
            "(U) Universal sortable: . . . {0:U} " +
            "(Y) Year: . . . . . . . . . . {0:Y} ", 
            thisDate);
        Console.WriteLine(s);

        // Format a Color enumeration value in various ways.
        Console.WriteLine("Standard Enumeration Format Specifiers");
        s = String.Format(
            "(G) General:. . . . . . . . . {0:G} " +
            "    (default):. . . . . . . . {0} (default = 'G') " +
            "(F) Flags:. . . . . . . . . . {0:F} (flags or integer) " +
            "(D) Decimal number: . . . . . {0:D} " +
            "(X) Hexadecimal:. . . . . . . {0:X} ", 
            Color.Green);       
        Console.WriteLine(s);
    }
}
以上代码的输出结果如下:
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: . . . . . . . 2007-1-29
(D) Long date:. . . . . . . . 2007年1月29日
(t) Short time: . . . . . . . 13:57
(T) Long time:. . . . . . . . 13:57:42
(f) Full date/short time: . . 2007年1月29日 13:57
(F) Full date/long time:. . . 2007年1月29日 13:57:42
(g) General date/short time:. 2007-1-29 13:57
(G) General date/long time: . 2007-1-29 13:57:42
(default):. . . . . . . . 2007-1-29 13:57:42 (default = 'G')
(M) Month:. . . . . . . . . . 1月29日
(R) RFC1123:. . . . . . . . . Mon, 29 Jan 2007 13:57:42 GMT
(s) Sortable: . . . . . . . . 2007-01-29T13:57:42
(u) Universal sortable: . . . 2007-01-29 13:57:42Z (invariant)
(U) Universal sortable: . . . 2007年1月29日 5:57:42
(Y) Year: . . . . . . . . . . 2007年1月
Standard Enumeration Format Specifiers
(G) General:. . . . . . . . . Green
(default):. . . . . . . . Green (default = 'G')
(F) Flags:. . . . . . . . . . Green (flags or integer)
(D) Decimal number: . . . . . 3
(X) Hexadecimal:. . . . . . . 00000003

String.Format()方法相关推荐

  1. String.format()方法使用说明

    2019独角兽企业重金招聘Python工程师标准>>> JDK1.5开始String类中提供了一个非常有用的方法String.format(String format, Object ...

  2. String.format()方法的使用

    转载自  java字符串格式化:String.format()方法的使用 常规类型的格式化 String类的format()方法用于创建格式化的字符串以及连接多个字符串对象.熟悉C语言的读者应该记得C ...

  3. String.format() 方法用法解说

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. String chargeFlowUrl = _AGENT_URL+ "?agentAcc ...

  4. Objective-C 字符串拼接函数 多个不同类型的参数拼接到一个字符串 类似于Java中 String.format()方法的原生API

    总目录 iOS开发笔记目录 从一无所知到入门 文章目录 需求 Screenshot Code Output 需求 我有多个参数(类型也许不同),需要拼接到一个字符串中. 在Java中有String.f ...

  5. java百分号的用法,Java String.format()方法中使用百分号'%'_栗子教程

    下面这个例子演示了如何在String.format方法中显示一个百分号. 像下面这样直接调用String.format("%d%", 100)来显示百分号,将会抛出java.uti ...

  6. String.format()方法详解

    String.format()方法详解 前言: String.format()作为文本处理工具,为我们提供强大而丰富的字符串格式化功能,这里根据查阅的资料做个学习笔记,整理成如下文章,供后续复习查阅. ...

  7. JAVA String.format 方法使用介绍

    在JDK1.5中,String类增加了静态方法format(String format, Objects... args),format(Local l ,String format, Objects ...

  8. JAVA String format 方法使用介绍

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 在JDK ...

  9. java字符串格式化:String.format()方法的使用

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

最新文章

  1. python上机实验报告读取文件_Python程序设计实验报告八 : 文件
  2. python如何导入txt文件-数据从txt文本导入python
  3. VC采集网页所有表单域
  4. C# UI界面的更新
  5. html按钮坐标,html-单选按钮位置CSS
  6. 字体大宝库:设计师必备的专业免费英文字体
  7. Python调用微博API获取微博内容
  8. 理论 | 教你彻底学会Java序列化和反序列化
  9. 政治经济学第一-三章脉络图
  10. tomcat启动war包_不用下载tomcat,maven插件直接运行war包,真香
  11. 自动化通讯协定——现场总线
  12. CCF NOI1005 存款收益
  13. 多种问题袭来:崩溃在边缘的“直播赚钱路”
  14. Python 快速验证代理IP是否有效
  15. 【bb平台刷课记】wireshark结合实例学抓包
  16. swagger设置字段required必填
  17. 高德地图获取坐标距离_计算两个坐标点之间的距离(高德地图)
  18. Flume+kafka+Spark Steaming demo2
  19. 3d建模网上学习靠谱吗?学3d建模哪个大学好?
  20. 私域运营中引流加爆微信好友的方法

热门文章

  1. mysql 脑裂的问题,DRBD脑裂问题故障处理
  2. Python Qt GUI设计:QTableView、QListView、QListWidet、QTableWidget、QTreeWidget和QTreeWidgetltem表格和树类(提升篇—1)
  3. Python Qt GUI设计:QLabel标签类(基础篇—11)
  4. LabVIEW仪表盘识别(实战篇—6)
  5. TinkerNode NB-IoT物联网开发板(NB-IoT专栏—拓展篇3)
  6. Keil5简介、下载及安装(NB-IoT专栏—基础篇2)
  7. Linux 内核启动流程
  8. P2P Device Discovery流程分析
  9. sysfs_create_dir_ns
  10. 【深度学习】(5) 简单网络,案例:服装图片分类,附python完整代码