⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chap21.lst

📁 Csharp2完全参考手册源代码 详细的说明可以在书里看到 该书是08年刚出炉很新鲜
💻 LST
📖 第 1 页 / 共 2 页
字号:
using System;  
  
class SubstringDemo {  
  public static void Main() {  
    string str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
 
    Console.WriteLine("str: " + str); 
     
    Console.Write("str.Substring(15): "); 
    string substr = str.Substring(15); 
    Console.WriteLine(substr); 
 
    Console.Write("str.Substring(0, 15): "); 
    substr = str.Substring(0, 15); 
    Console.WriteLine(substr); 
  } 
}

listing 12
// Demonstrate various format specifiers. 
  
using System;  
  
class FormatDemo {  
  public static void Main() {  
    double v = 17688.65849;  
    double v2 = 0.15;  
    int x = 21;  
   
    Console.WriteLine("{0:F2}", v);  
  
    Console.WriteLine("{0:N5}", v);  
  
    Console.WriteLine("{0:e}", v);  
  
    Console.WriteLine("{0:r}", v);  
  
    Console.WriteLine("{0:p}", v2);  
  
    Console.WriteLine("{0:X}", x);  
  
    Console.WriteLine("{0:D12}", x);  
 
    Console.WriteLine("{0:C}", 189.99);  
  }  
}

listing 13
using System;   
   
class FormatDemo2 {   
  public static void Main() {   
 
    // Format the same argument three different ways:    
    Console.WriteLine("{0:F2}  {0:F3}  {0:e}", 10.12345);   
   
    // Display arguments in non-sequential order. 
    Console.WriteLine("{2:d} {0:d} {1:d}", 1, 2, 3);   
  }   
}

listing 14
// Use String.Format() to format a value.  
  
using System;  
  
class FormatDemo {  
  public static void Main() {  
    double v = 17688.65849;  
    double v2 = 0.15;  
    int x = 21;  
   
    string str = String.Format("{0:F2}", v);  
    Console.WriteLine(str);  
  
    str = String.Format("{0:N5}", v);  
    Console.WriteLine(str);  
  
    str = String.Format("{0:e}", v);  
    Console.WriteLine(str);  
  
    str = String.Format("{0:r}", v);  
    Console.WriteLine(str);  
  
    str = String.Format("{0:p}", v2);  
    Console.WriteLine(str);  
  
    str = String.Format("{0:X}", x);  
    Console.WriteLine(str);  
  
    str = String.Format("{0:D12}", x);  
    Console.WriteLine(str);  
 
    str = String.Format("{0:C}", 189.99); 
    Console.WriteLine(str); 
  }  
}

listing 15
// A closer look at Format(). 
  
using System;  
  
class FormatDemo2 {  
  public static void Main() {  
    int i; 
    int sum = 0; 
    int prod = 1; 
    string str; 
 
    /* Display the running sum and product 
       for the numbers 1 through 10. */ 
    for(i=1; i <= 10; i++) { 
      sum += i; 
      prod *= i; 
      str = String.Format("Sum:{0,3:D}  Product:{1,8:D}", 
                          sum, prod); 
      Console.WriteLine(str); 
    } 
  } 
}

listing 16
// Use ToString() to format values. 
 
using System; 
 
class ToStringDemo { 
  public static void Main() { 
    double v = 17688.65849; 
    double v2 = 0.15; 
    int x = 21; 
  
    string str = v.ToString("F2"); 
    Console.WriteLine(str); 
 
    str = v.ToString("N5"); 
    Console.WriteLine(str); 
 
    str = v.ToString("e"); 
    Console.WriteLine(str); 
 
    str = v.ToString("r"); 
    Console.WriteLine(str); 
 
    str = v2.ToString("p"); 
    Console.WriteLine(str); 
 
    str = x.ToString("X"); 
    Console.WriteLine(str); 
 
    str = x.ToString("D12"); 
    Console.WriteLine(str); 
 
    str = 189.99.ToString("C"); 
    Console.WriteLine(str); 
  } 
}

listing 17
// Using custom formats. 
  
using System;  
  
class PictureFormatDemo {  
  public static void Main() {  
    double num = 64354.2345; 
 
    Console.WriteLine("Default format: " + num); 
 
    // Display with 2 decimal places. 
    Console.WriteLine("Value with two decimal places: " + 
                      "{0:#.##}", num); 
 
    // Display with commas and 2 decimal places. 
    Console.WriteLine("Add commas: {0:#,###.##}", num); 
  
    // Display using scientific notation. 
    Console.WriteLine("Use scientific notation: " + 
                      "{0:#.###e+00}", num); 
 
    // Scale the value by 1000. 
    Console.WriteLine("Value in 1,000s: " + 
                      "{0:#0,}", num); 
 
    /* Display positive, negative, and zero 
       values differently. */ 
    Console.WriteLine("Display positive, negative, " + 
                      "and zero values differently."); 
    Console.WriteLine("{0:#.#;(#.##);0.00}", num); 
    num = -num; 
    Console.WriteLine("{0:#.##;(#.##);0.00}", num); 
    num = 0.0; 
    Console.WriteLine("{0:#.##;(#.##);0.00}", num); 
 
    // Display a percentage. 
    num = 0.17;     
    Console.WriteLine("Display a percentage: {0:#%}", num); 
  } 
}

listing 18
// Format time and date information. 
  
using System;  
  
class TimeAndDateFormatDemo {  
  public static void Main() {  
    DateTime dt = DateTime.Now; // obtain current time 
 
    Console.WriteLine("d format: {0:d}", dt); 
    Console.WriteLine("D format: {0:D}", dt); 
 
    Console.WriteLine("t format: {0:t}", dt); 
    Console.WriteLine("T format: {0:T}", dt); 
 
    Console.WriteLine("f format: {0:f}", dt); 
    Console.WriteLine("F format: {0:F}", dt); 
 
    Console.WriteLine("g format: {0:g}", dt); 
    Console.WriteLine("G format: {0:G}", dt); 
 
    Console.WriteLine("m format: {0:m}", dt); 
    Console.WriteLine("M format: {0:M}", dt); 
 
    Console.WriteLine("r format: {0:r}", dt); 
    Console.WriteLine("R format: {0:R}", dt); 
 
    Console.WriteLine("s format: {0:s}", dt); 
 
    Console.WriteLine("u format: {0:u}", dt); 
    Console.WriteLine("U format: {0:U}", dt); 
 
    Console.WriteLine("y format: {0:y}", dt); 
    Console.WriteLine("Y format: {0:Y}", dt); 
  } 
}

listing 19
// A simple clock. 
  
using System;  
  
class SimpleClock {  
  public static void Main() {  
    string t; 
    int seconds; 
 
    DateTime dt = DateTime.Now; 
    seconds = dt.Second; 
 
    for(;;) { 
      dt = DateTime.Now; 
 
      // update time if seconds change 
      if(seconds != dt.Second) { 
        seconds = dt.Second; 
 
        t = dt.ToString("T"); 
 
        if(dt.Minute==0 && dt.Second==0)  
          t = t + "\a"; // ring bell at top of hour 
 
        Console.WriteLine(t); 
      } 
    } 
  } 
}

listing 20
// Format time and date information. 
  
using System;  
  
class CustomTimeAndDateFormatsDemo {  
  public static void Main() {  
    DateTime dt = DateTime.Now; 
 
    Console.WriteLine("Time is {0:hh:mm tt}", dt); 
    Console.WriteLine("24 hour time is {0:HH:mm}", dt); 
    Console.WriteLine("Date is {0:ddd MMM dd, yyyy}", dt); 
 
    Console.WriteLine("Era: {0:gg}", dt); 
 
    Console.WriteLine("Time with seconds: " + 
                      "{0:HH:mm:ss tt}", dt); 
 
    Console.WriteLine("Use m for day of month: {0:m}", dt); 
    Console.WriteLine("use m for minutes: {0:%m}", dt); 
  } 
}

listing 21
// Format an enumeration. 
 
using System; 
 
class EnumFmtDemo { 
  enum Direction { North, South, East, West } 
  [Flags] enum Status { Ready=0x1, OffLine=0x2,  
                        Waiting=0x4, TransmitOK=0x8, 
                        RecieveOK=0x10, OnLine=0x20 } 
 
  public static void Main() { 
    Direction d = Direction.West; 
 
    Console.WriteLine("{0:G}", d); 
    Console.WriteLine("{0:F}", d); 
    Console.WriteLine("{0:D}", d); 
    Console.WriteLine("{0:X}", d); 
     
    Status s = Status.Ready | Status.TransmitOK; 
 
    Console.WriteLine("{0:G}", s); 
    Console.WriteLine("{0:F}", s); 
    Console.WriteLine("{0:D}", s); 
    Console.WriteLine("{0:X}", s); 
  } 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -