C#StringBuilder - 如何转义此字符串:

时间:2022-07-02 22:25:04
<document.write("<SCR"+"IPT TYPE='text/javascript' SRC='"+"http"+(window.location.protocol.indexOf('https:')==0?'s':'')+"://"+gDomain+"/"+gDcsId+"/wtid.js"+"'><\/SCR"+"IPT>");

I need to escape the string above in order to add the whole thing to a StringBuilder but so far I must be missing something because string termination is not correct...

我需要转义上面的字符串,以便将整个事物添加到StringBuilder,但到目前为止我必须遗漏一些东西,因为字符串终止不正确...

4 个解决方案

#1


4  

string x = @"<document.write(""<SCR""+""IPT TYPE=""'text/javascript' SRC='""+""http""+(window.location.protocol.indexOf('https:')==0?'s':'')+""://""+gDomain+""/""+gDcsId+""/wtid.js""+""'><\/SCR""+""IPT>"");";

The @ prefix makes escaping simpler. You just have to turn each " into "".

@前缀使转义更简单。你只需要把每个“变成”“。

You will find your program much easier to maintain if you store the JavaScript in an external file. I assume you're using StringBuilder so you can mix bits of constant script with a few dynamic values? You could write it in a file but put escapes like this for the dynamic values:

如果将JavaScript存储在外部文件中,您会发现您的程序更容易维护。我假设您正在使用StringBuilder,因此您可以将一些常量脚本与一些动态值混合使用?你可以把它写在一个文件中,但是为这个动态值设置这样的转义:

var fromCSharp = {0};

Then at runtime, load the JS file and give it to string.Format as the format string, along with values to replace each occurrence of {0}, {1}, etc. You only need to load the format string from the file once and keep it cached.

然后在运行时,加载JS文件并将其作为格式字符串提供给string.Format,以及替换每次出现的{0},{1}等的值。您只需要从文件中加载一次格式字符串并保持缓存。

Also if the values you are inserting into the JavaScript are themselves string literals, you will need to escape them according to the syntax of JavaScript.

此外,如果您插入JavaScript的值本身就是字符串文字,则需要根据JavaScript的语法对它们进行转义。

#2


6  

You should try something like this :

你应该尝试这样的事情:

@"<document.write(""<SCR""+""IPT TYPE='text/javascript' SRC='""+""http""+(window.location.protocol.indexOf('https:')==0?'s':'')+""://""+gDomain+""/""+gDcsId+""/wtid.js""+""'><\/SCR""+""IPT>"");"

When prefixing a string literal with @, the only escaping needed is to double the " caracter.

在使用@作为字符串文字的前缀时,唯一需要的转义是将“caracter”加倍。

Hope this help.

希望这有帮助。

#3


0  

The string in the beginning is what I want exactly... (I'm not mixing up JavaScript with C# - I just need to add a string to at C# StringBuilder that by coincidance contains some JavaScript)

开头的字符串就是我想要的......(我不是把JavaScript和C#混在一起 - 我只需要在C#StringBuilder中添加一个字符串,其中包含一些JavaScript)

Its an external script that I have to put on a page, using a StringBuilder (for various reasons).

它是一个外部脚本,我必须使用StringBuilder放在页面上(出于各种原因)。

I have no way of knowing if any changes to the script will make it fail so I have to include it as is...

我无法知道脚本的任何更改是否会使其失败,所以我必须按原样包含它...

It's only 1 line of the total script, but most other lines I've managed to escape correctly and they are included as wanted...

它只是整个脚本中的一行,但是我设法正确逃脱的大多数其他行并且它们被包含在内...

#4


0  

I think you are mixing up what is JavaScript and what is C#. Can you please tell us the string you are string you are trying to achieve...

我认为你混淆了什么是JavaScript和什么是C#。你能告诉我们你想要实现的字符串吗?

for instance

window.location.protocol.indexOf('https:') is JavaScript

window.location.protocol.indexOf('https:')是JavaScript

but presumably

gDomain and gDcsId

gDomain和gDcsId

are variables from your C# method

是来自C#方法的变量

maybe this:

"<SCRIPT TYPE='text/javascript' SRC='"+"http"+"(window.location.protocol.indexOf('https:')==0?'s':'')"+"://" + gDomain + "/"+ gDcsId+ "/wtid.js"+"'></SCRIPT>")

#1


4  

string x = @"<document.write(""<SCR""+""IPT TYPE=""'text/javascript' SRC='""+""http""+(window.location.protocol.indexOf('https:')==0?'s':'')+""://""+gDomain+""/""+gDcsId+""/wtid.js""+""'><\/SCR""+""IPT>"");";

The @ prefix makes escaping simpler. You just have to turn each " into "".

@前缀使转义更简单。你只需要把每个“变成”“。

You will find your program much easier to maintain if you store the JavaScript in an external file. I assume you're using StringBuilder so you can mix bits of constant script with a few dynamic values? You could write it in a file but put escapes like this for the dynamic values:

如果将JavaScript存储在外部文件中,您会发现您的程序更容易维护。我假设您正在使用StringBuilder,因此您可以将一些常量脚本与一些动态值混合使用?你可以把它写在一个文件中,但是为这个动态值设置这样的转义:

var fromCSharp = {0};

Then at runtime, load the JS file and give it to string.Format as the format string, along with values to replace each occurrence of {0}, {1}, etc. You only need to load the format string from the file once and keep it cached.

然后在运行时,加载JS文件并将其作为格式字符串提供给string.Format,以及替换每次出现的{0},{1}等的值。您只需要从文件中加载一次格式字符串并保持缓存。

Also if the values you are inserting into the JavaScript are themselves string literals, you will need to escape them according to the syntax of JavaScript.

此外,如果您插入JavaScript的值本身就是字符串文字,则需要根据JavaScript的语法对它们进行转义。

#2


6  

You should try something like this :

你应该尝试这样的事情:

@"<document.write(""<SCR""+""IPT TYPE='text/javascript' SRC='""+""http""+(window.location.protocol.indexOf('https:')==0?'s':'')+""://""+gDomain+""/""+gDcsId+""/wtid.js""+""'><\/SCR""+""IPT>"");"

When prefixing a string literal with @, the only escaping needed is to double the " caracter.

在使用@作为字符串文字的前缀时,唯一需要的转义是将“caracter”加倍。

Hope this help.

希望这有帮助。

#3


0  

The string in the beginning is what I want exactly... (I'm not mixing up JavaScript with C# - I just need to add a string to at C# StringBuilder that by coincidance contains some JavaScript)

开头的字符串就是我想要的......(我不是把JavaScript和C#混在一起 - 我只需要在C#StringBuilder中添加一个字符串,其中包含一些JavaScript)

Its an external script that I have to put on a page, using a StringBuilder (for various reasons).

它是一个外部脚本,我必须使用StringBuilder放在页面上(出于各种原因)。

I have no way of knowing if any changes to the script will make it fail so I have to include it as is...

我无法知道脚本的任何更改是否会使其失败,所以我必须按原样包含它...

It's only 1 line of the total script, but most other lines I've managed to escape correctly and they are included as wanted...

它只是整个脚本中的一行,但是我设法正确逃脱的大多数其他行并且它们被包含在内...

#4


0  

I think you are mixing up what is JavaScript and what is C#. Can you please tell us the string you are string you are trying to achieve...

我认为你混淆了什么是JavaScript和什么是C#。你能告诉我们你想要实现的字符串吗?

for instance

window.location.protocol.indexOf('https:') is JavaScript

window.location.protocol.indexOf('https:')是JavaScript

but presumably

gDomain and gDcsId

gDomain和gDcsId

are variables from your C# method

是来自C#方法的变量

maybe this:

"<SCRIPT TYPE='text/javascript' SRC='"+"http"+"(window.location.protocol.indexOf('https:')==0?'s':'')"+"://" + gDomain + "/"+ gDcsId+ "/wtid.js"+"'></SCRIPT>")