1.使用变量
Customer customer =new Customer();
customer.FirstName="Jack";
customer.LastName="Smith";
旧版:string a =String.Format("FirstName is {0},LastName is {1}.", customer.FirstName , customer.LastName);
新版:string a =String.Format("FirstName is \ {customer.FirstName} , LastName is \ {customer.LastName}");
2.时间
string time =String.Format("{0:yyyy-MM-dd}",DateTime.Now);
string time=$"{DateTime.Now:yyyy-MM-dd}";
3.$
string
s1 = $"{name},{age}"; string
s2 = $"姓名={name},年龄={age}";
本文介绍了C#中字符串格式化的几种方法,包括使用变量、时间格式化等,并对比了新旧版本的不同写法。
677

被折叠的 条评论
为什么被折叠?



