C# 如何在C中使用replace命令?

C# 如何在C中使用replace命令?,c#,C#,这是我的代码,但我无法替换resultValue Replace方法采用精确的字符串 在原件中,您有: static void Main(string[] args) { string t="<html> <head> <link rel='stylesheet' type='text/css' href='http://www.taxmann.com/css/taxmannstyle.css' /> </head> <bod

这是我的代码,但我无法替换resultValue

Replace方法采用精确的字符串

在原件中,您有:

static void Main(string[] args)
{

    string t="<html>  <head> <link rel='stylesheet' type='text/css' href='http://www.taxmann.com/css/taxmannstyle.css' />  </head>  <body ><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 of the current financial year.</div>\r\n\r\n</div>\r\n</body>\r\n</html>\r\n</body></html></body></html>"



    string resultValue = t.Replace("<html><head> <link rel='stylesheet' type='text/css' href='http://www.taxmann.com/css/taxmannstyle.css' /></head> <body ><html>", " <html><head> </head> <body ><html>");  //its not working

    Console.WriteLine(resultValue);

} 
 />  </head>
但在替换字符串中,您有:

static void Main(string[] args)
{

    string t="<html>  <head> <link rel='stylesheet' type='text/css' href='http://www.taxmann.com/css/taxmannstyle.css' />  </head>  <body ><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 of the current financial year.</div>\r\n\r\n</div>\r\n</body>\r\n</html>\r\n</body></html></body></html>"



    string resultValue = t.Replace("<html><head> <link rel='stylesheet' type='text/css' href='http://www.taxmann.com/css/taxmannstyle.css' /></head> <body ><html>", " <html><head> </head> <body ><html>");  //its not working

    Console.WriteLine(resultValue);

} 
 />  </head>
空间是重要的

在本例中,由于这是HTML,我建议使用HTML解析器而不是string.Replace


两种流行的解析器是and。您可以使用其中任何一个来读取HTML并重写所需内容。

您使用它的方式很好,但它与您的输入字符串不匹配,因为短语与。空格使替换失败


您可以使用正则表达式来匹配允许标记之间空白的位置,或者可以在>之后去掉所有空白,然后替换值。

t和t中的空格符号。替换模式不匹配。请抽象您的问题,而不是在此处复制输入。没有替换,因为不匹配。不匹配除了其他人所说的之外,您的代码甚至无法编译。下次请尝试发布编译并运行拼写检查的代码。顺便说一下,这不是有效的html文档。这个原始HTML我需要replacestring resultValue=t.Replace;使用thispastebin.com/LyyJdMHQ我需要的这个原始HTMLreplace@user2148026请先替换\r\n,然后替换要替换的代码。标记之间不要包含空格,因为pastebin上的代码也没有空格。