【问题标题】:How to replace   to space?如何将 替换为空格?
【发布时间】:2010-09-14 12:13:30
【问题描述】:

内容是

    Hello World.

<a&nbsp;href="#"&nbsp;target=_blank>hello&nbsp;World</a>

如何替换html代码中的&amp;nbsp;,并在文本中保留另一个&amp;nbsp;

【问题讨论】:

  • 如果可能,您应该尽量避免将&amp;nbsp; 首先放入您的html 代码中。
  • 是的,你是怎么遇到这种情况的?向我们展示该代码。
  • 您使用什么程序来创建该标记?
  • 看这个帖子:stackoverflow.com/questions/3705996/mysql-replace-spaces我执行 content = content.Replace(" ", " ");

标签: c# regex


【解决方案1】:

对我来说最好的是:

Imports System.Web
HttpUtility.HtmlDecode(codeHtml)

【讨论】:

  • 留下答案时,尝试做两件事很重要....确保您的答案解决了所提出的问题,并解释了您的解决方案解决问题的原因。这很重要,因为并非所有阅读此问题的人都是专家,并且无法了解您的解决方案如何解决问题。考虑编辑您的答案,尝试解释它是如何解决问题的。
  • 这实际上是最好的答案,虽然解释得不好。请参阅 MSDN 文档了解 HtmlDecode 的实际作用。
  • 这并不能完全回答问题。它将&amp;nbsp; 转换为不等于字符串比较中的正常空格的不间断空格(好)(最初问的是什么问题?)
【解决方案2】:

你可以尝试搜索

(?<=<[^>]*)&nbsp;

并用一个空格替换它?

这会在标签内查找&amp;nbsp;(前面有&lt;,可能还有除&gt; 之外的其他字符)。

不过,这非常脆弱。例如,如果字符串/属性中有&lt;/&gt; 符号,它将失败。最好一开始就避免让那些&amp;nbsp; 进入错误的位置。

【讨论】:

  • RegEx 进行简单替换?这看起来像是试图用核弹射杀一只苍蝇:)
  • @Andrey:这不是一个简单的替换。您可能想再次阅读问题的最后一行。 ;-)
【解决方案3】:

【讨论】:

  • 同意,在这种情况下他应该使用正则表达式。
  • 虽然这在理论上可以回答问题,it would be preferable 在这里包含答案的基本部分,并提供链接以供参考。
【解决方案4】:
string A = HttpContext.Current.Server.HtmlDecode(Text);

string A = Text.Replace("&nbsp"," ");

string A = Text.Replace("&amp;nbsp;", " ");

                           ↑ &amp;nbsp;

【讨论】:

    【解决方案5】:

    这将找到所有包含 &nbsp: 的文本条

    <[^>]+?&nbsp;[^<]+?>
    

    从这里你可以用空格做一个简单的字符串替换,因为正则表达式会给你文本中匹配的位置。

    【讨论】:

      【解决方案6】:

      只需将 &nbsp 替换为 string.Empty 之后的 Text Like below..

      xyz.Text.Replace("&amp;nbsp;", string.Empty);

      【讨论】:

        【解决方案7】:

        //功能!!!!!!!!!!!!!!!

        字符串 a =UnHtml(文本);

        //--------------------------------------------- ------

         private static readonly Regex _tags_ = new Regex(@"<[^>]+?>", RegexOptions.Multiline | RegexOptions.Compiled);
        
            //add characters that are should not be removed to this regex
            private static readonly Regex _notOkCharacter_ = new Regex(@"[^\w;&#@.:/\\?=|%!() -]", RegexOptions.Compiled);
        
            public static String UnHtml(String html)
            {
                html = HttpUtility.UrlDecode(html);
                html = HttpUtility.HtmlDecode(html);
        
                html = RemoveTag(html, "<!--", "-->");
                html = RemoveTag(html, "<script", "</script>");
                html = RemoveTag(html, "<style", "</style>");
        
                //replace matches of these regexes with space
                html = _tags_.Replace(html, " ");
                html = _notOkCharacter_.Replace(html, " ");
                html = SingleSpacedTrim(html);
        
                return html;
            }
        
            private static String RemoveTag(String html, String startTag, String endTag)
            {
                Boolean bAgain;
                do
                {
                    bAgain = false;
                    Int32 startTagPos = html.IndexOf(startTag, 0, StringComparison.CurrentCultureIgnoreCase);
                    if (startTagPos < 0)
                        continue;
                    Int32 endTagPos = html.IndexOf(endTag, startTagPos + 1, StringComparison.CurrentCultureIgnoreCase);
                    if (endTagPos <= startTagPos)
                        continue;
                    html = html.Remove(startTagPos, endTagPos - startTagPos + endTag.Length);
                    bAgain = true;
                } while (bAgain);
                return html;
            }
        
            private static String SingleSpacedTrim(String inString)
            {
                StringBuilder sb = new StringBuilder();
                Boolean inBlanks = false;
                foreach (Char c in inString)
                {
                    switch (c)
                    {
                        case '\r':
                        case '\n':
                        case '\t':
                        case ' ':
                            if (!inBlanks)
                            {
                                inBlanks = true;
                                sb.Append(' ');
                            }   
                            continue;
                        default:
                            inBlanks = false;
                            sb.Append(c);
                            break;
                    }
                }
                return sb.ToString().Trim();
            }
        

        https://newbedev.com/remove-html-tags-from-string-including-nbsp-in-c

        【讨论】:

          猜你喜欢
          • 2012-05-15
          • 1970-01-01
          • 2015-05-25
          • 1970-01-01
          • 1970-01-01
          • 2011-07-12
          • 2013-06-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多