我想在此字符串中插入字符串变量( myVar )如何执行此操作?
breadCrumbLit.Text = $@"<a href='VARIABLE HERE'>VARIABLE HERE</a>
i class='fa fa-angle-right'></i>
<span> VARIABLE HERE </span>
<i class='fa fa-angle-right'></i>
<a href='/EN/Pages/SGHG.aspx'> VARIABLE HERE </a>
<i class='fa fa-angle-right'></i>
<span> VARIABLE HERE </span>";
语法给我错误,我哪里错了?
答案 0 :(得分:1)
您收到代码编译错误的原因是@
不是正确的字符串。您正在使用@
指定字符串代码是多行,但在代码中您有不同的字符串。
这是你的:
#1 #2 #3
breadCrumbLit.Text = @"<a href='/EN'>'" + myVar + "'</a>
<i class='fa fa-angle-right'></i>";
将@
放在包含多行的字符串中:
breadCrumbLit.Text = "<a href='/EN'>'" + myVar + @"'</a>
<i class='fa fa-angle-right'></i>";
更好的方法是使用string.Format
:
breadCrumbLit.Text = string.Format(@"<a href='/EN'>'{0}'</a>
<i class='fa fa-angle-right'></i>",myVar);
或者它的C#6.0语法:
breadCrumbLit.Text = = $@"<a href='/EN'>'{myVar}'</a>
<i class='fa fa-angle-right'></i>";
要编辑问题:缺少用{}
包装参数。检查一下:
string variable1, variable2, variable3, variable4, variable5;
variable1 = variable2 = variable3 = variable4 = variable5 = "some text";
var Text = $@"<a href='{variable1}'>{variable2}</a>
i class='fa fa-angle-right'></i>
<span> {variable3} </span>
<i class='fa fa-angle-right'></i>
<a href='/EN/Pages/SGHG.aspx'> {variable4} </a>
<i class='fa fa-angle-right'></i>
<span> {variable5} </span>";
答案 1 :(得分:0)
您可以使用escape:
breadCrumbLit.Text = "<a href=\"/EN\">" + myVar +
"</a> <i class=\"fa fa-angle-right\"></i>";
答案 2 :(得分:0)
为此纠正代码。我发现没有语法错误。
breadCrumbLit .Text = @"<a href='/EN'>'" + myVar + "'</a><i class='fa fa-angle-right'></i>";