gpt4 book ai didi

c# - 如何将我的 C# 代码转换为 js 方法替换

转载 作者:行者123 更新时间:2023-12-02 17:37:59 30 4
gpt4 key购买 nike

在 C# 中我有这行代码

this.Office.Name.Replace(@"""", @"\""")

我想在js中使用这个字符串

我尝试过这样的:

  1. var test = <%=this.Office.Name.Replace(@"""", @"\"""); %>;
    Compiler Error Message: CS1026: ) expected Line 574:
    string test = <%=this.Office.Name.Replace(@"""", @"\"""); %>;

  2. 没有@

    string test = <%=this.Office.Name.Replace("""", "\"""); %>;

    Compiler Error Message: CS1010: Newline in constant

  3. string test = <%=this.Office.Name.Replace('"', '\"'); %>;

    Compiler Error Message: CS1026: ) expected

最佳答案

因为它是用于声明变量的 javascript var (反斜杠应该有效)

var test = <%=this.Office.OfficeName.Replace("\"", "\\\"") %>;  
// NO SEMICOLON in the asp.net TAGS <%= ... %>
// 3 Backslashes the first one escapes the Second one,
// the Second is literal(since it was escaped),
// the third escapes the double quot.

但我会查看可能的值,以确保不存在任何边缘情况,这种转换可能不起作用。

如果您想使用 Javascript 而不是使用 C# 进行替换,请使用 Pid 的答案

关于c# - 如何将我的 C# 代码转换为 js 方法替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22454260/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com