js function as string inside StringBuilder C#

huangapple go评论74阅读模式
英文:

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(@&quot;&lt;script&gt;
       var vm = new Vue({{
        el: &#39;main_div&#39;,
        data() {{ 
            return {{
                proddetails: [],
            }}
        }},
        created: function () {{
            this.proddetails = {{ jsonobjectplaceholder }};
        }},
    }});
&lt;/script&gt;&quot;, &quot;&quot;);

Alternatively you can use Append instead of AppendFormat. Try like below and it will work.

StringBuilder sb = new StringBuilder();

sb.Append(@&quot;&lt;script&gt;
       var vm = new Vue({
        el: &#39;main_div&#39;,
        data() { 
            return {
                proddetails: [],
            }
        },
        created: function () {
            this.proddetails = { jsonobjectplaceholder };
        },
    });
&lt;/script&gt;&quot;);

答案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(&quot;{0}, {1}, {2}&quot;, &quot;var1&quot;, &quot;var2&quot;, &quot;var3&quot;);

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.

See Here

string exampleString = $@&quot;This
is a
string&quot;;

So the code you want looks like the following:

StringBuilder sb = new StringBuilder();
sb.Append(@&quot;&lt;script&gt; var s = &#39;myStringValue&#39;; &lt;/script&gt;&quot;);

huangapple
  • 本文由 发表于 2023年7月11日 12:34:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76658734.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定