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);   //取出是上午还是下午     
         }

  

原文地址:https://www.cnblogs.com/liuyudong0825/p/4935749.html