英文:
js function as string inside StringBuilder C#
问题
我是 .net 平台的新手,我想知道如何将带花括号的 js 函数作为字符串传递给 StringBuilder.AppendFormat() c#。我想要做的如下所示
StringBuilder sb = new StringBuilder();
sb.AppendFormat(@"<script>
var vm = new Vue({
el: 'main_div',
data() {
return {
proddetails: [],
}
},
created: function () {
this.proddetails = { jsonobjectplaceholder };
},
});
</script>");
如果有任何愚蠢之处请原谅,先行感谢您的帮助!期待得到一个合理的答复
英文:
I'm new in .net platform and I'm curious about how can I pass js function with curly brackets as string inside StringBuilder.AppendFormat() c#.here is what I'm willing to do
StringBuilder sb = new StringBuilder();
sb.AppendFormat(@"<script>
var vm = new Vue({
el: 'main_div',
data() {
return {
proddetails: [],
}
},
created: function () {
this.proddetails = { jsonobjectplaceholder };
},
});
</script>");
sorry for stupidity if any and thanks in advance for the attention!
expecting a reasonable answer
答案1
得分: 1
您正在使用AppendFormat
,其中第一个参数是格式化字符串。如果您希望在其中使用花括号,那么您需要将它们转义为双花括号{{
和}}
。请尝试如下。
参考链接:[在String.Format中转义花括号'{' ] 1
sb.AppendFormat(@"
<script>
var vm = new Vue({{
el: 'main_div',
data() {{
return {{
proddetails: [],
}}
}},
created: function () {{
this.proddetails = {{ jsonobjectplaceholder }};
}},
}});
</script>", "");
或者,您可以使用Append
而不是AppendFormat
。请尝试如下,它也可以工作。
StringBuilder sb = new StringBuilder();
sb.Append(@"
<script>
var vm = new Vue({
el: 'main_div',
data() {
return {
proddetails: [],
}
},
created: function () {
this.proddetails = { jsonobjectplaceholder };
},
});
</script>");
英文:
You are using AppendFormat
where first parameter is Formatted string. And if you wish to use curly braces inside it then you need to escape them double braces {{
and }}
. Try like below.
Referene : Escape curly brace '{' in String.Format
sb.AppendFormat(@"<script>
var vm = new Vue({{
el: 'main_div',
data() {{
return {{
proddetails: [],
}}
}},
created: function () {{
this.proddetails = {{ jsonobjectplaceholder }};
}},
}});
</script>", "");
Alternatively you can use Append
instead of AppendFormat
. Try like below and it will work.
StringBuilder sb = new StringBuilder();
sb.Append(@"<script>
var vm = new Vue({
el: 'main_div',
data() {
return {
proddetails: [],
}
},
created: function () {
this.proddetails = { jsonobjectplaceholder };
},
});
</script>");
答案2
得分: 0
Your code contains 'AppendFormat', did you mean to use 'Append'?
'StringBuilder.AppendFormat'用于创建格式化的字符串,其中使用了一个字符串数组。
例如:
sb.AppendFormat("{0}, {1}, {2}", "var1", "var2", "var3");
上述代码将生成:"var1, var2, var3"
有关StringBuilder.AppendFormat的更多信息,请参阅此处:查看此处
您可以使用'@'符号作为逐字字符串标识符,这将允许您在字符串中包含多行。
string exampleString = $@"This
is a
string";
因此,您想要的代码如下:
StringBuilder sb = new StringBuilder();
sb.Append(@"<script> var s = 'myStringValue'; </script>");
英文:
Your code contains 'AppendFormat', did you mean to use 'Append'?
'StringBuilder.AppendFormat' is used for creating a string that is formatted with an array of strings.
For instance:
sb.AppendFormat("{0}, {1}, {2}", "var1", "var2", "var3");
The above will result in: "var1, var2, var3"
See more about StringBuilder.AppendFormat here: See Here
You can use the '@' symbol as a verbatim identifier which will allow you to include multiple lines in your string.
string exampleString = $@"This
is a
string";
So the code you want looks like the following:
StringBuilder sb = new StringBuilder();
sb.Append(@"<script> var s = 'myStringValue'; </script>");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论