我有一个带有HTML代码的字符串。我想删除所有的HTML标签。所以<和>之间的所有字符。
这是我的代码片段:
WebClient wClient = new WebClient();
SourceCode = wClient.DownloadString( txtSourceURL.Text );
txtSourceCode.Text = SourceCode;
//remove here all between "<" and ">"
txtSourceCodeFormatted.Text = SourceCode;
希望有人能帮助我
试试这个:
txtSourceCodeFormatted.Text = Regex.Replace(SourceCode, "<.*?>", string.Empty);
但是,正如其他人提到的那样,handle with care。
根据Ravi's answer,您可以使用
string noHTML = Regex.Replace(inputHTML, @"<[^>]+>| ", "").Trim();
要么
string noHTMLNormalised = Regex.Replace(noHTML, @"\s{2,}", " ");