C#格式化字符串 官网
C#里格式化字符串很实用,常用的是String.Format方法。它允许将变量插入到字符串特定位置。 string name = "Tom";age = 20;string formattedStr = String.Format("我的名字是 {0},年龄是 {1}。", name, age); // 运行结果: 我的名字是 Tom,年龄是 20。Console.WriteLine(formattedStr); 还能使用字符串插值,用$符号,更简洁直观。 string newName = "Jerry";int newAge = 25;string interpolatedStr = $"我的名字是 {newName},年龄是 {newAge}。"; // 运行结果: 我的名字是 Jerry,年龄是 25。Console.WriteLine(interpolatedStr); 使用字符串插值时,变量要放在{}内,且前面有$符号。