-8
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);

} 

これは私のコードですが、交換できませんresultValue

4

2 に答える 2

2

Replaceメソッドは正確な文字列を取ります。

オリジナルには次のものがあります。

 />  </head>

しかし、置換文字列には次のものがあります。

 /></head>

スペースは重要です。

この場合、これは HTML であるため、代わりに HTML パーサーを使用することをお勧めしますstring.Replace

人気のある 2 つのパーサーは、HTML Agility PackCsQueryです。いずれかを使用して HTML を読み取り、必要なものを書き直します。

于 2013-04-09T08:05:28.460 に答える
2

使用方法は問題ありませんが、フレーズ<html> <head>が と同じではないため、入力文字列と一致しません<html><head>。スペースがあると、置換が失敗します。

正規表現を使用して、タグ間の空白を許可する場所を照合するか、後のすべての空白を取り除い>てから値を置き換えることができます。

于 2013-04-09T08:05:36.010 に答える