Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > .NET > C#.NET > String.Format()方法
【标  题】:String.Format()方法
【关键字】:String.Format
【来  源】:http://blog.csdn.net/Charlie_Lee/archive/2007/01/29/1496999.aspx

String.Format()方法

Your Ad Here

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
 
在线程中访问UI控件:【上一篇】
我的.NET c# 软件开发框架(一):【下一篇】
【相关文章】
  • CString.Format(_T())
  • 【随机文章】
  • 学习ARM开发(14)
  • 四种排序方法的比较
  • window.showModalDialog以及window.open用法简介
  • 用Visual C#来修改和删除数据库记录
  • Perl的面向对象编程
  • GCC 手册(续)
  • JetNuke:管理,二次开发与Velocity简介(中文版)
  • 电视设备的数字化(1)
  • NDIS wrapper for FreeBSD 5.2 CURRENT
  • 用WinGrub 引导Linux 简述
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.