C# 6.0 $"Hello {csdn}"

最后更新于:2022-04-01 20:22:47

“hello $world”的格式化字符串是指把字符串中一个单词,以一个标示开头。可以代换为单词所指的变量。 这个在jq有,而C#string的格式只能用格式的字符占位符,格式的字符占位符都是数字,这样多了很容易混,好多我都出现了,拷贝代码,然后没有排好数字,漏了一个数字,这样出现了错误。 `string.Format("这里有很长字符串{0}{2}" , "Hello" , "csdn");` ![string.Format("这里有很长字符串{0}{2}" , "Hello" , "csdn");](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-04-08_5707636782643.jpg "") 而看到一个大神实现了类似jQueryStringFormat的扩展string,觉得C#内置有一个方法: ~~~ var csdn = "csdn"; var result = $"Hello {csdn}"; Console.Write(result); ~~~ 会输出![输出Hello csdn](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-04-08_5707636797025.jpg "") 通过$开头字符串,中间{}作为变量名,可以把字符串代换为变量的字符。 代码:[https://code.csdn.net/lindexi_gd/lindexi_gd/tree/master/hellow_$csdn](https://code.csdn.net/lindexi_gd/lindexi_gd/tree/master/hellow_$csdn) 参考:[http://www.cnblogs.com/isaboy/p/4945045.html](http://www.cnblogs.com/isaboy/p/4945045.html)
';