C# 6.0 $"Hello {csdn}"

“hello $world”的格式化字符串是指把字符串中一个单词,以一个标示开头。可以代换为单词所指的变量。
这个在jq有,而C#string的格式只能用格式的字符占位符,格式的字符占位符都是数字,这样多了很容易混,好多我都出现了,拷贝代码,然后没有排好数字,漏了一个数字,这样出现了错误。
string.Format("这里有很长字符串{0}{2}" , "Hello" , "csdn");
string.Format("这里有很长字符串{0}{2}" , "Hello" , "csdn");
而看到一个大神实现了类似jQueryStringFormat的扩展string,觉得C#内置有一个方法:

            var csdn = "csdn";
            var result = $"Hello {csdn}";
            Console.Write(result);

会输出输出Hello csdn

通过$开头字符串,中间{}作为变量名,可以把字符串代换为变量的字符。

代码:https://code.csdn.net/lindexi_gd/lindexi_gd/tree/master/hellow_$csdn

参考:http://www.cnblogs.com/isaboy/p/4945045.html

原文地址:https://www.cnblogs.com/lindexi/p/12087726.html