您的位置:首页 > 编程语言 > ASP

学用 ASP.Net 之 "字符串" (2): string.Format

2013-05-07 16:13 447 查看



一般应用与左右对齐:

protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.TextMode = TextBoxMode.MultiLine;

string str;
str = string.Format("***{0}***{1}***", "ASP.Net", 3.5);
TextBox1.Text += str + "\n"; //***ASP.Net***3.5***

str = string.Format("*{0,10}*", "asp");
TextBox1.Text += str + "\n"; //*       asp*

str = string.Format("*{0,-10}*", "asp");
TextBox1.Text += str + "\n"; //*asp       *

TextBox1.Text += string.Format("{0,3} : {1,-3}\n", "BC", 123); // BC : 123
TextBox1.Text += string.Format("{0,3} : {1,-3}\n", "C", 12);   //  C : 12
TextBox1.Text += string.Format("{0,3} : {1,-3}\n", "ABC", 1);  //ABC : 1
}


标准数字格式:

protected void Button1_Click(object sender, EventArgs e)
{
string s1,s2,s3,s4,s5,s6,s7,s8,s9,s0;
double num = 2.5;
s1 = string.Format("Cc {0:c}", num);      //Cc ¥2.50
s2 = string.Format("Dd {0:d}", (int)num); //Dd 2
s3 = string.Format("E  {0:E}", num);      //E  2.500000E+000
s4 = string.Format("e  {0:e}", num);      //e  2.500000e+000
s5 = string.Format("Ff {0:f}", num);      //Ff 2.50
s6 = string.Format("Gg {0:g}", num);      //Gg 2.5
s7 = string.Format("Nn {0:n}", num);      //Nn 2.50
s8 = string.Format("Pp {0:p}", num);      //Pp 250.00%
s9 = string.Format("Rr {0:r}", num);      //Rr 2.5
s0 = string.Format("Xx {0:x}", (int)num); //Xx 2

string br = "\n";
TextBox1.Text = string.Concat(s1, br, s2, br, s3, br, s4, br, s5, br, s6, br, s7, br, s8, br, s9, br, s0);
}


标准数字格式的精度:

protected void Button1_Click(object sender, EventArgs e)
{
string s1,s2,s3,s4,s5,s6,s7,s8,s9,s0;
double num = 2.5;
s1 = string.Format("Cc {0:c3}", num);      //Cc ¥2.500
s2 = string.Format("Dd {0:d3}", (int)num); //Dd 002
s3 = string.Format("E  {0:E3}", num);      //E  2.500E+000
s4 = string.Format("e  {0:e3}", num);      //e  2.500e+000
s5 = string.Format("Ff {0:f3}", num);      //Ff 2.500
s6 = string.Format("Gg {0:g3}", num);      //Gg 2.5
s7 = string.Format("Nn {0:n3}", num);      //Nn 2.500
s8 = string.Format("Pp {0:p3}", num);      //Pp 250.000%
s9 = string.Format("Rr {0:r3}", num);      //Rr 2.5
s0 = string.Format("Xx {0:x3}", (int)num); //Xx 002

string br = "\n";
TextBox1.Text = string.Concat(s1, br, s2, br, s3, br, s4, br, s5, br, s6, br, s7, br, s8, br, s9, br, s0);
}


自定义的数字格式:

protected void Button1_Click(object sender, EventArgs e)
{
string s1,s2,s3,s4,s5,s6,s7,s8,s9,s0;
double num = -1234.567;
s1 = string.Format("{0:000000.00}", num);         //-001234.57
s2 = string.Format("{0:######.##}", num);         //-1234.57
s3 = string.Format("{0:#,#.####}", num);          //-1,234.567
s4 = string.Format("{0:0,0.0000}", num);          //-1,234.5670
s5 = string.Format("{0:######.000000}", num);     //-1234.567000
s6 = string.Format("{0:000000.######}", num);     //-001234.567
s7 = string.Format("{0:#.00%}", num);             //-123456.70%
s8 = string.Format("{0:%#.00}", num);             //-%123456.70
s9 = string.Format("{0:(+|0)#.#;(-)#.#}", num);   //(-)1234.6
s0 = string.Format("{0:(+)#.#;(-)#.#;(0)}", num); //(-)1234.6

string br = "\n";
TextBox1.Text = string.Concat(s1, br, s2, br, s3, br, s4, br, s5, br, s6, br, s7, br, s8, br, s9, br, s0);
}


标准日期和时间格式:

protected void Button1_Click(object sender, EventArgs e)
{
string[] arr = new string[16];
DateTime dt = new DateTime(2011, 1, 2, 13, 24, 56, 789);
arr[0]  = string.Format("   {0}", dt);   //   2011/1/2 13:24:56
arr[1]  = string.Format("d  {0:d}", dt); //d  2011/1/2
arr[2]  = string.Format("D  {0:D}", dt); //D  2011年1月2日
arr[3]  = string.Format("f  {0:f}", dt); //f  2011年1月2日 13:24
arr[4]  = string.Format("F  {0:F}", dt); //F  2011年1月2日 13:24:56
arr[5]  = string.Format("g  {0:g}", dt); //g  2011/1/2 13:24
arr[6]  = string.Format("G  {0:G}", dt); //G  2011/1/2 13:24:56
arr[7]  = string.Format("Mm {0:m}", dt); //Mm 1月2日
arr[8]  = string.Format("Oo {0:o}", dt); //Oo 2011-01-02T13:24:56.7890000
arr[9]  = string.Format("Rr {0:r}", dt); //Rr Sun, 02 Jan 2011 13:24:56 GMT
arr[10] = string.Format("s  {0:s}", dt); //s  2011-01-02T13:24:56
arr[11] = string.Format("t  {0:t}", dt); //t  13:24
arr[12] = string.Format("T  {0:T}", dt); //T  13:24:56
arr[13] = string.Format("u  {0:u}", dt); //u  2011-01-02 13:24:56Z
arr[14] = string.Format("U  {0:U}", dt); //U  2011年1月2日 5:24:56
arr[15] = string.Format("Yy {0:y}", dt); //Yy 2011年1月

string str = "";
foreach (string s in arr) { str += s + "\n"; }
TextBox1.Text = str;
}


自定义的日期和时间格式:

protected void Button1_Click(object sender, EventArgs e)
{
string [] arr = new string[44];
DateTime dt = new DateTime(2011, 1, 2, 13, 24, 56, 789);
arr[0]  = string.Format("d       {0:d}", dt);                  //d       2011/1/2
arr[1]  = string.Format("dd      {0:dd}", dt);                 //dd      02
arr[2]  = string.Format("ddd     {0:ddd}", dt);                //ddd     周日
arr[3]  = string.Format("dddd    {0:dddd}", dt);               //dddd    星期日
arr[4]  = string.Format("f       {0:f}", dt);                  //f       2011年1月2日 13:24
arr[5]  = string.Format("ff      {0:ff}", dt);                 //ff      78
arr[6]  = string.Format("fff     {0:fff}", dt);                //fff     789
arr[7]  = string.Format("ffff    {0:ffff}", dt);               //ffff    7890
arr[8]  = string.Format("fffff   {0:fffff}", dt);              //fffff   78900
arr[9]  = string.Format("ffffff  {0:ffffff}", dt);             //ffffff  789000
arr[10] = string.Format("fffffff {0:fffffff}", dt);            //fffffff 7890000
arr[11] = string.Format("F       {0:F}", dt);                  //F       2011年1月2日 13:24:56
arr[12] = string.Format("FF      {0:FF}", dt);                 //FF      78
arr[13] = string.Format("FFF     {0:FFF}", dt);                //FFF     789
arr[14] = string.Format("FFFF    {0:FFFF}", dt);               //FFFF    789
arr[15] = string.Format("FFFFF   {0:FFFFF}", dt);              //FFFFF   789
arr[16] = string.Format("FFFFFF  {0:FFFFFF}", dt);             //FFFFFF  789
arr[17] = string.Format("FFFFFFF {0:FFFFFFF}", dt);            //FFFFFFF 789
arr[18] = string.Format("g       {0:g}", dt);                  //g       2011/1/2 13:24
arr[19] = string.Format("h       {0:%h}", dt);                 //h       1
arr[20] = string.Format("hh      {0:hh}", dt);                 //hh      01
arr[21] = string.Format("H       {0:%H}", dt);                 //H       13
arr[22] = string.Format("HH      {0:HH}", dt);                 //HH      13
arr[23] = string.Format("K       {0:%K}", dt);                 //K
arr[24] = string.Format("m       {0:m}", dt);                  //m       1月2日
arr[25] = string.Format("mm      {0:mm}", dt);                 //mm      24
arr[26] = string.Format("M       {0:M}", dt);                  //M       1月2日
arr[27] = string.Format("MM      {0:MM}", dt);                 //MM      01
arr[28] = string.Format("MMM     {0:MMM}", dt);                //MMM     一月
arr[29] = string.Format("MMMM    {0:MMMM}", dt);               //MMMM    一月
arr[30] = string.Format("s       {0:s}", dt);                  //s       2011-01-02T13:24:56
arr[31] = string.Format("ss      {0:ss}", dt);                 //ss      56
arr[32] = string.Format("t       {0:t}", dt);                  //t       13:24
arr[33] = string.Format("tt      {0:tt}", dt);                 //tt      下午
arr[34] = string.Format("y       {0:y}", dt);                  //y       2011年1月
arr[35] = string.Format("yy      {0:yy}", dt);                 //yy      11
arr[36] = string.Format("yyy     {0:yyy}", dt);                //yyy     2011
arr[37] = string.Format("yyyy    {0:yyyy}", dt);               //yyyy    2011
arr[38] = string.Format("yyyyy   {0:yyyyy}", dt);              //yyyyy   02011
arr[39] = string.Format("z       {0:%z}", dt);                 //z       +8
arr[40] = string.Format("zz      {0:zz}", dt);                 //zz      +08
arr[41] = string.Format("zzz     {0:zzz}", dt);                //zzz     +08:00
arr[42] = string.Format("{0:yyyy年MM月dd日 HH时mm分ss秒}", dt); //2011年01月02日 13时24分56秒
arr[43] = string.Format("{0:yyyy/M/d H:m:s}", dt);             //2011/1/2 13:24:56

string str = "";
foreach (string s in arr) { str += s + "\n"; }
TextBox1.Text = str;
}


按区域格式化:

// 需 using System.Globalization;
protected void Button1_Click(object sender, EventArgs e)
{
string s1, s2, s3, s4, s5, s6, s7, s8, s9, s0;
double num = 2.5;
DateTime dt = new DateTime(2011, 1, 2, 13, 24, 56, 789);
s1 = string.Format("{0:c}", num);                           //¥2.50
s2 = string.Format(new CultureInfo("en-US"), "{0:c}", num); //$2.50
s3 = string.Format(new CultureInfo("fr-FR"), "{0:c}", num); //2,50 €
s4 = string.Format(new CultureInfo("it-IT"), "{0:c}", num); //€ 2,50
s5 = string.Format(new CultureInfo("de-DE"), "{0:c}", num); //2,50 €
s6 = string.Format("{0,-5:ddd}\t{0:dddd}", dt);                           //周日   	星期日
s7 = string.Format(new CultureInfo("en-US"), "{0,-5:ddd}\t{0:dddd}", dt); //Sun  	Sunday
s8 = string.Format(new CultureInfo("fr-FR"), "{0,-5:ddd}\t{0:dddd}", dt); //dim. 	dimanche
s9 = string.Format(new CultureInfo("it-IT"), "{0,-5:ddd}\t{0:dddd}", dt); //dom  	domenica
s0 = string.Format(new CultureInfo("de-DE"), "{0,-5:ddd}\t{0:dddd}", dt); //So   	Sonntag

string br = "\n";
TextBox1.Text = string.Concat(s1, br, s2, br, s3, br, s4, br, s5, br, s6, br, s7, br, s8, br, s9, br, s0);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: