我正在开发一个ASP.NET 4.0 Web应用程序,它要做的主要目标是转到MyURL变量中的URL 然后从上到下读取它,搜索以"description"开头的所有行,并且只保留那些删除所有HTML标记.我接下来要做的是从结果后面删除"描述"文本,所以我只剩下我的设备名称.我该怎么做?
protected void parseButton_Click(object sender, EventArgs e)
{
MyURL = deviceCombo.Text;
WebRequest objRequest = HttpWebRequest.Create(MyURL);
objRequest.Credentials = CredentialCache.DefaultCredentials;
using (StreamReader objReader = new StreamReader(objRequest.GetResponse().GetResponseStream()))
{
originalText.Text = objReader.ReadToEnd();
}
//Read all lines of file
String[] crString = { "<BR> " };
String[] aLines = originalText.Text.Split(crString, StringSplitOptions.RemoveEmptyEntries);
String noHtml = String.Empty;
for (int x = 0; x < aLines.Length; x++)
{
if (aLines[x].Contains(filterCombo.SelectedValue))
{
noHtml += (RemoveHTML(aLines[x]) + "\r\n");
}
}
//Print results to textbox
resultsBox.Text = String.Join(Environment.NewLine, noHtml);
}
public static string RemoveHTML(string text)
{
text = text.Replace(" ", " ").Replace("<br>", "\n");
var oRegEx = new System.Text.RegularExpressions.Regex("<[^>]+>");
return oRegEx.Replace(text, string.Empty);
}
Run Code Online (Sandbox Code Playgroud)
好的,所以我想出了如何通过我现有的一个功能删除单词:
public static string RemoveHTML(string text)
{
text = text.Replace(" ", " ").Replace("<br>", "\n").Replace("description", "").Replace("INFRA:CORE:", "")
.Replace("RESERVED", "")
.Replace(":", "")
.Replace(";", "")
.Replace("-0/3/0", "");
var oRegEx = new System.Text.RegularExpressions.Regex("<[^>]+>");
return oRegEx.Replace(text, string.Empty);
}
Run Code Online (Sandbox Code Playgroud)
小智 5
public static void Main(String[] args)
{
string str = "He is driving a red car.";
Console.WriteLine(str.Replace("red", "").Replace(" ", " "));
}
Run Code Online (Sandbox Code Playgroud)
输出:他正在开车。
注意:在第二个替换它的双空格。
链接:https : //i.stack.imgur.com/rbluf.png
试试这个。它将删除所有出现的要删除的单词。
| 归档时间: |
|
| 查看次数: |
38327 次 |
| 最近记录: |