C#占位符和格式化字符串


static void Main()  
         {
                   string c=Console.ReadLine();
                   string d=Console.ReadLine();
                  Console.WriteLine(c+","+d);    //用“+”連接符
         }

  

你說這樣寫很容易寫錯,很麻煩,C#還提供另一種書寫方式,就是占位符,用{ }來表示,在{ }內填寫所占的位的序號,C#規定從0開始,也就是說剛才那中輸出,我們還可以這樣來表示
Response.Write(“{0},{1}”,c,d); 
在這里有兩個位c,d,那么也就需要兩個占位符所以我們寫成{0},{1},還需要注意的是,占位符要寫在””內。
static void Main()  
         {
                   string c=Console.ReadLine();
                   string d=Console.ReadLine();
                   string m=String.Format(“{0},{1}”,c,d);   //字符串格式輸出
                   Response.Write(m);   
         }

  

可以看出輸出結果是完全一樣的。在這里String是一個類,Format是其中的一個方法用來格式化輸出字符。
我們知道在現實的生活中有時候需要特殊的表示字符,例如表示貨幣,時間,那該怎么辦呢?不用擔心,C#中又格式化標識符,下面給大家介紹幾個常用的格式化標識符
字母 含義 
C或c   Currency  貨幣格式 
D或d  Decimal  十進制格式(十進制整數,不要和.Net的Decimal數據類型混淆了) 
E或e  Exponent  指數格式 
F或f  Fixed point  固定精度格式 
G或g    General  常用格式 
N或n    用逗號分割千位的數字,比如1234將會被變成1,234 
P或p  Percentage  百分符號格式 
R或r  Round-trip  圓整(只用於浮點數)保證一個數字被轉化成字符串以后可以再被轉回成同樣的數字 
X或x   16進制格式
protected void Page_Load(object sender, EventArgs e)    
         {
                   int i=12345;
                   Response.Write("{0:C}",i);   //貨幣
                   Response.Write("{0:D}",i);   //十進制數
                   Response.Write("{0:E}",i);    //科學技術法
                   Response.Write("{0:F}",i);   // 浮點數表示法
                   Response.Write("{0:G}",i);   //G或g General 常用格式
                   Response.Write("{0:N}",i);   //N或n 用逗號分割千位的數字
         }

  

1.數值的處理與格式化
   當檢查用戶輸入的數據類型是哪一種數據時,只需要調用值類型的TryParse()方法來檢查數據內容。
   TryParse()和Parse()方法的區別是,前者會返回一個bool值,后者如轉換不成功,則拋出異常,需要用try catch語句捕捉。
   下面是檢查用戶輸入內容是否為數字
    protected void Page_Load(object sender, EventArgs e)
    {
        string s1 = "三百";
        string s2 = "1000";
        string s3 = "2,000";
        string s4 = "-1000";
        string s5 = "4000d";
        int currentValue = 0;
        Response.Write(int.TryParse(s1, out currentValue));     //返回False
        Response.Write(int.TryParse(s2, out currentValue));     //返回True
        Response.Write(int.TryParse(s3, out currentValue));     //返回False
        Response.Write(int.TryParse(s4, out currentValue));     //返回True
        Response.Write(int.TryParse(s5, out currentValue));     //返回False     
    }

  

數值的格式化:
    若有decimal和float類型數處理,則要使用后置字符M或F,如:
    decimal d = 12345.67M;
    float f =3.1415F;
    否則,會把數字視為 double 類型處理,產生編譯錯誤,若是整數加不加M都可以。
2.字母的處理與格式化
protected void Page_Load(object sender, EventArgs e)
    {
        string str = "Do you like ASP.NET?";
        Response.Write(str.ToUpper());      //轉換為大寫
        Response.Write(str.ToLower());      //轉換為小寫
    }

  d MM/dd/yyyy ShortDatePattern(短日期模式) 
D dddd,MMMM dd,yyyy LongDatePattern(長日期模式) 
F dddd,MMMM dd,yyyy HH:mm Full date and time (long date and short time)(全日期和時間模式) 
F dddd,MMMM dd,yyyy HH:mm:ss FullDateTimePattern (long date and long time)(長日期和長時間) 
G MM/dd/yyyy HH:mm General (short date and short time)(通用模式,短日期和短時間) 
G MM/dd/yyyy HH:mm:ss General (short date and long time)(通用模式,短日期和長時間) 
M,M MMMM dd MonthDayPattern(月天模式) 
r,R ddd,dd MMM yyyy,HH':'mm':'ss 'GMT' RFC1123Pattern (RFC1123模式) 
S yyyy-MM-dd HH:mm:ss SortableDateTimePattern (conforms to ISO 8601) using local time(使用本地時間的可排序模式) 
T HH:mm ShortTimePattern (短時間模式) 
T HH:mm:ss LongTimePattern(長時間模式) 
U yyyy-MM-dd HH:mm:ss UniversalSortable-DateTimePattern (conforms to ISO 8601) using universal time(通用可排序模式) 
U dddd,MMMM dd,yyyy,HH:mm:ss UniversalSortable-DateTimePattern(通用可排序模式) 
y,Y MMMM,yyyy YearMonthPattern(年月模式)

static void Main()   
         {
                  Response.Write("{0:D}",DateTime.Now);   //輸出到天
                   Response.Write("{0:y}",DateTime.Now);   //輸出到月
                   Response.Write("{0:m}",DateTime.Now);    //取出是那個月
                   Response.Write("{0:T}",DateTime.Now);   // 取長時間到秒
                   Response.Write("{0:t}",DateTime.Now);   //取短時間到分
                   Response.Write("{0:tt}",DateTime.Now);   //取出是上午還是下午     
         }

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM