5.6C#引用类型之字符串(String)

字符串声明

string 标识符 = "值";

获取字符串长度

a.Length

内插字符串

若要将字符串标识为内插字符串,可在该字符串前面加上 $ 符号。 字符串文本开头的 $ 和 " 之间不能有任何空格。

string name = "Mark";
var date = DateTime.Now;

// 复合格式字符串
Console.WriteLine("Hello, {0}! Today is {1}, it's {2:HH:mm} now.", name, date.DayOfWeek, date);
// 内插字符串
Console.WriteLine($"Hello, {name}! Today is {date.DayOfWeek}, it's {date:HH:mm} now.");
// 两种方式同样的输出
// Hello, Mark! Today is Wednesday, it's 19:40 now.

原创文章,作者:huoxiaoqiang,如若转载,请注明出处:https://www.huoxiaoqiang.com/csharp/csbasic/7945.html

发表评论

登录后才能评论