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

字符串声明

string 标识符 = "值";

获取字符串长度

a.Length

字符串连接

可使用 + 运算符 或 += 运算符连接字符串。

string userName = "<Type your name here>";
string dateString = DateTime.Today.ToShortDateString();
string str = "Hello " + userName + ". Today is " + dateString + ".";
str += " How are you today?";

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

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/csharplang/7945.html

发表评论

登录后才能评论