简体   繁体   English

将\\替换为string.empty不起作用

[英]Replace \ with string.empty is not working

I am using Aspose.Words to convert a Word document to HTML and finally into a string containing all the HTML for the document. 我正在使用Aspose.Words将Word文档转换为HTML,最后转换为包含该文档所有HTML的字符串。

What I am having trouble with is replacing the "\\" . 我遇到的麻烦是替换"\\"

Here is the string I get from the document: 这是我从文档中获得的字符串:

htmlString  "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><meta http-equiv=\"Content-Style-Type\" content=\"text/css\" /><meta name=\"generator\" content=\"Aspose.Words for .NET 13.7.0.0\" /><title></title></head><body><div><p style=\"font-size:12pt; line-height:115%; margin:0pt 0pt 10pt\"><span style=\"color:#ff0000; font-family:Calibri; font-size:12pt; font-weight:bold\">Evaluation Only. Created with Aspose.Words. Copyright 2003-2011 Aspose Pty Ltd.</span></p><h2 style=\"font-size:13pt; line-height:115%; margin:10pt 0pt 0pt; page-break-after:avoid; page-break-inside:avoid\"><span style=\"color:#4f81bd; font-family:Cambria; font-size:13pt; font-weight:bold\">This is a header</span></h2><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 10pt\"><span style=\"font-family:Calibri; font-size:11pt\">&#xa0;</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 10pt\"><span style=\"font-family:Calibri; font-size:11pt\">Look at my awesome bulleted list.</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 10pt\"><span style=\"font-family:Calibri; font-size:11pt\">&#xa0;</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 0pt 18pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">1)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Option 1</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 0pt 36pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">a)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Sup Option A</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 0pt 36pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">b)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Sub Option B</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 0pt 36pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">c)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Sub Option C</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 0pt 18pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">2)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Option 2</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 0pt 36pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">a)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Sub Option A</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 0pt 54pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">i)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Sub </span><span style=\"font-family:Calibri; font-size:11pt\">Sub</span><span style=\"font-family:Calibri; font-size:11pt\"> Option </span><span style=\"font-family:Calibri; font-size:11pt\">i</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 0pt 36pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">b)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Sub Option B</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 0pt 54pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">i)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Sub </span><span style=\"font-family:Calibri; font-size:11pt\">Sub</span><span style=\"font-family:Calibri; font-size:11pt\"> Option </span><span style=\"font-family:Calibri; font-size:11pt\">i</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 10pt 54pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">ii)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Sub </span><span style=\"font-family:Calibri; font-size:11pt\">Sub</span><span style=\"font-family:Calibri; font-size:11pt\"> Option ii</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 10pt\"><span style=\"font-family:Calibri; font-size:11pt\">&#xa0;</span></p><p style=\"border-bottom-color:#4f81bd; border-bottom-style:solid; border-bottom-width:0.5pt; font-size:11pt; line-height:115%; margin:10pt 46.8pt 14pt; padding-bottom:4pt\"><span style=\"color:#4f81bd; font-family:Calibri; font-size:11pt; font-style:italic; font-weight:bold\">This is another heading</span></p></div></body></html>"   string

As you can see it has a whole bunch of =\\" I need to remove all of the \\'s and replace it with String.Empty. 如您所见,它有一堆==”,我需要删除所有\\,然后将其替换为String.Empty。

Here is the code I am using: 这是我正在使用的代码:

string htmlString = doc.ToString(SaveFormat.Html);
string text = htmlString.ToString().Replace(@"\", string.Empty);

However when I hover over text or add a watch for text the entire string still displays all of the \\'s . 然而,当我将鼠标悬停在text或添加手表text整个字符串仍然显示所有的\\'s What am I doing wrong here? 我在这里做错了什么?

Here is the watch value for text: 这是文本的监视值:

text    "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><meta http-equiv=\"Content-Style-Type\" content=\"text/css\" /><meta name=\"generator\" content=\"Aspose.Words for .NET 13.7.0.0\" /><title></title></head><body><div><p style=\"font-size:12pt; line-height:115%; margin:0pt 0pt 10pt\"><span style=\"color:#ff0000; font-family:Calibri; font-size:12pt; font-weight:bold\">Evaluation Only. Created with Aspose.Words. Copyright 2003-2011 Aspose Pty Ltd.</span></p><h2 style=\"font-size:13pt; line-height:115%; margin:10pt 0pt 0pt; page-break-after:avoid; page-break-inside:avoid\"><span style=\"color:#4f81bd; font-family:Cambria; font-size:13pt; font-weight:bold\">This is a header</span></h2><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 10pt\"><span style=\"font-family:Calibri; font-size:11pt\">&#xa0;</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 10pt\"><span style=\"font-family:Calibri; font-size:11pt\">Look at my awesome bulleted list.</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 10pt\"><span style=\"font-family:Calibri; font-size:11pt\">&#xa0;</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 0pt 18pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">1)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Option 1</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 0pt 36pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">a)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Sup Option A</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 0pt 36pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">b)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Sub Option B</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 0pt 36pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">c)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Sub Option C</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 0pt 18pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">2)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Option 2</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 0pt 36pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">a)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Sub Option A</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 0pt 54pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">i)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Sub </span><span style=\"font-family:Calibri; font-size:11pt\">Sub</span><span style=\"font-family:Calibri; font-size:11pt\"> Option </span><span style=\"font-family:Calibri; font-size:11pt\">i</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 0pt 36pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">b)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Sub Option B</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 0pt 54pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">i)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Sub </span><span style=\"font-family:Calibri; font-size:11pt\">Sub</span><span style=\"font-family:Calibri; font-size:11pt\"> Option </span><span style=\"font-family:Calibri; font-size:11pt\">i</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 10pt 54pt; text-indent:-18pt\"><span style=\"font-family:Calibri; font-size:11pt\">ii)</span><span style=\"font:7.0pt 'Times New Roman'\">&#xa0;&#xa0;&#xa0;&#xa0;&#xa0;&#xa0; </span><span style=\"font-family:Calibri; font-size:11pt\">Sub </span><span style=\"font-family:Calibri; font-size:11pt\">Sub</span><span style=\"font-family:Calibri; font-size:11pt\"> Option ii</span></p><p style=\"font-size:11pt; line-height:115%; margin:0pt 0pt 10pt\"><span style=\"font-family:Calibri; font-size:11pt\">&#xa0;</span></p><p style=\"border-bottom-color:#4f81bd; border-bottom-style:solid; border-bottom-width:0.5pt; font-size:11pt; line-height:115%; margin:10pt 46.8pt 14pt; padding-bottom:4pt\"><span style=\"color:#4f81bd; font-family:Calibri; font-size:11pt; font-style:italic; font-weight:bold\">This is another heading</span></p></div></body></html>"   string

Those aren't actually in the string. 这些实际上不在字符串中。

The debugger is showing a string literal for the string, including escape characters. 调试器将显示该字符串的字符串文字,包括转义字符。

To see the raw value of the string, use the visualizer (the button on the left) 要查看字符串的原始值,请使用可视化器(左侧的按钮)

The \\ s aren't actually there. \\实际上并不存在。 It's just the display showing you that the " isn't the end of the string's contents 只是显示向您显示"不是字符串内容的结尾”

However when I hover over text or add a watch for text the entire string still displays all of the \\'s. 但是,当我将鼠标悬停在文本上或添加文本监视时,整个字符串仍显示所有\\。 What am I doing wrong here? 我在这里做错了什么?

This is just a matter of how the debugger displays values. 这只是调试器如何显示值的问题。 The actual text is: 实际的文本是:

<html><head><meta http-equiv="Content-Type"

But the debugger escapes the quotes with \\" 但是调试器使用\\"转义引号

The reason is because it is showing you the string value representation of that string... In strings, to display the double quote character, you first have to escape it with the \\ char. 原因是因为它正在向您显示该字符串的字符串值表示形式。在字符串中,要显示双引号字符,您首先必须使用\\ char对其进行转义。 If you output it to the console you shouldn't see the escape ch 如果将其输出到控制台,则不应看到转义符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM