【问题标题】:how to replace in C# from HTML如何在 C# 中从 HTML 替换
【发布时间】:2021-03-29 14:14:45
【问题描述】:
    "<html><body style='background-color:Black;font-size:30px;color:#fff;'>
<html>\r\n<head><link href='http://www.taxmann.com/TaxmannWhatsnewService/Styles/style.css' rel='stylesheet' type='text/css' />\r\n
<title>Finmin Aims to Halve Net Bad Loans of PSBs</title>\r\n<style
 type=\"text/css\">\r\nbody{font-family:Arial, Helvetica, sans-serif; font-size:12px; line-height:18px;text-align:justify;}\r\n.w100{width:100%;}\r\n.fl-l{float:left;}\r\n.ffla{font-family:Arial,
 Helvetica, sans-serif;}\r\n.fs18{font-size:18px;}\r\n.mart10{margin-top:10px;}\r\n.fcred{color:#c81616;}\r\n.tc{text-align:center;}\r\n.tu{text-transform:uppercase;}\r\n.lh18{line-height:18px;}\r\n</style>\r\n</head>\r\n<body>\r\n<div class=\"w100 fl-l\">\r\n<div class=\"w100 fl-l ffla fs18 mart10 fcred ttunderline tc tu\">Finmin Aims to Halve Net Bad Loans of PSBs</div>\r\n\r\n<div class=\"w100 fl-l lh18 mart10\">Concernedover the rising bad loans of the state-run banks, the finance ministry is working out a plan to reduce their net non-performing assets (NPAs) to 1% of net advances by the end  Baand strict recovery policy. – www.economictimes.indiatimes.com</div>\r\n\r\n</div>\r\n</body>\r\n</html>\r\n</body><
/html>"

这是我的 HTML,我想从给定的 HTML 中替换或删除 <html>\r\n<head><link href='http://www.taxmann.com/TaxmannWhatsnewService/Styles/style.css' rel='stylesheet' type='text/css' />\r\

我尝试了以下代码:

string t= html.replace(" given remove   ", " ")

但做不到 请帮助我如何做到这一点?

【问题讨论】:

  • 我建议使用 HtmlAgilityPack 将您的 HTML 解析为 DOM,然后以这种方式操作节点,而不是将 HTML 文档视为不透明的字符串。
  • 另外,以后尽量不要使用Regex解析HTML。
  • @AgentFire:他没有使用正则表达式,是吗? Replace 是标准的字符串方法。
  • 我必须替换 \r\n \r\n in这个 Html 那我们不能为此应用替换命令吗?
  • @GeorgeDuckett 专门为你提到的“未来”。

标签: c# string


【解决方案1】:

首先不要对此类要求使用字符串替换,而是检查并使用HtmlAgilityPack。其次,我认为由于quick give me 的情况和/或依赖于其他一些实用工具,您可能不想投入时间。

无论如何,here 都是你的例子。

示例代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace TestApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string inputValue = "<html><body style='background-color:Black;font-size:30px;color:#fff;'><html>\r\n<head><link href='http://www.taxmann.com/TaxmannWhatsnewService/Styles/style.css' rel='stylesheet' type='text/css' />\r\n<title>Finmin Aims to Halve Net Bad Loans of PSBs</title>\r\n<style type=\"text/css\">\r\nbody{font-family:Arial, Helvetica, sans-serif; font-size:12px; line-height:18px;text-align:justify;}\r\n.w100{width:100%;}\r\n.fl-l{float:left;}\r\n.ffla{font-family:Arial, Helvetica, sans-serif;}\r\n.fs18{font-size:18px;}\r\n.mart10{margin-top:10px;}\r\n.fcred{color:#c81616;}\r\n.tc{text-align:center;}\r\n.tu{text-transform:uppercase;}\r\n.lh18{line-height:18px;}\r\n</style>\r\n</head>\r\n<body>\r\n<div class=\"w100 fl-l\">\r\n<div class=\"w100 fl-l ffla fs18 mart10 fcred ttunderline tc tu\">Finmin Aims to Halve Net Bad Loans of PSBs</div>\r\n\r\n<div class=\"w100 fl-l lh18 mart10\">Concernedover the rising bad loans of the state-run banks, the finance ministry is working out a plan to reduce their net non-performing assets (NPAs) to 1% of net advances by the end  Baand strict recovery policy. – www.economictimes.indiatimes.com</div>\r\n\r\n</div>\r\n</body>\r\n</html>\r\n</body></html>";
            string resultValue= inputValue.Replace("href='http://www.taxmann.com/TaxmannWhatsnewService/Styles/style.css' rel='stylesheet' type='text/css' />\r\n", " ");
            Console.WriteLine("<span>Input value: </span>");
            Console.WriteLine(inputValue);
            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine("");
            Console.WriteLine("<span>Output value: </span>");
            Console.WriteLine(resultValue);
        }
    }
}

【讨论】:

猜你喜欢
  • 2011-10-17
  • 1970-01-01
  • 2011-01-22
  • 1970-01-01
  • 2023-03-30
  • 2011-09-26
  • 2015-12-11
  • 2015-09-11
  • 1970-01-01
相关资源
最近更新 更多