c#中如何将字符串转化为内插字符串比如My name is name and I am age years old转换为$My name is name and I am age years old
在C#中将字符串转换为内插字符串有两种方法:
-
使用字符串插值(String Interpolation):
string name = "John"; int age = 25; string interpolatedString = $"My name is {name} and I am {age} years old."; -
使用
string.Format方法:string name = "John"; int age = 25; string formatString = "My name is {0} and I am {1} years old."; string interpolatedString = string.Format(formatString, name, age);
无论使用哪种方法,最终都可以得到同样的结果。
原文地址: https://www.cveoy.top/t/topic/ikVj 著作权归作者所有。请勿转载和采集!