从字符串中删除单词c#

发布于 2024-12-08 16:49:05 字数 1428 浏览 0 评论 0 原文

我正在开发一个 ASP.NET 4.0 Web 应用程序,它的主要目标是转到 MyURL 变量中的 URL,然后从上到下读取它,搜索以“描述”,并且仅在删除所有 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>&nbsp;" };
        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("&nbsp;", " ").Replace("<br>", "\n");
        var oRegEx = new System.Text.RegularExpressions.Regex("<[^>]+>");
        return oRegEx.Replace(text, string.Empty);
    }

I am working on a ASP.NET 4.0 web application, the main goal for it to do is go to the URL in the MyURL variable then read it from top to bottom, search for all lines that start with "description" and only keep those while removing all HTML tags. What I want to do next is remove the "description" text from the results afterwords so I have just my device names left. How would I do this?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

走过海棠暮 2024-12-15 16:49:05

好吧,我想出了如何通过我现有的函数之一删除这些单词:

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

Ok so I figured out how to remove the words through one of my existing functions:

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);
}
妖妓 2024-12-15 16:49:05
public static void Main(String[] args)
{
    string str = "He is driving a red car.";

    Console.WriteLine(str.Replace("red", "").Replace("  ", " "));
}   

输出:
他正在开车。

注意:在第二个中将其替换为双空格。

链接:https://i.sstatic.net/rbluf.png

试试这个。它将删除您要删除的单词的所有出现位置。

public static void Main(String[] args)
{
    string str = "He is driving a red car.";

    Console.WriteLine(str.Replace("red", "").Replace("  ", " "));
}   

Output:
He is driving a car.

Note: In the second Replace its a double space.

Link : https://i.sstatic.net/rbluf.png

Try this.It will remove all occurrence of the word which you want to remove.

临风闻羌笛 2024-12-15 16:49:05

使用 LINQ 尝试这样的操作:

List<string> lines = new List<string>{
"Hello world",
"Description: foo",
"Garbage:baz",
"description purple"};

 //now add all your lines from your html doc.
 if (aLines[x].Contains(filterCombo.SelectedValue))
 {
       lines.Add(RemoveHTML(aLines[x]) + "\r\n");
 }

var myDescriptions = lines.Where(x=>x.ToLower().BeginsWith("description"))
                          .Select(x=> x.ToLower().Replace("description",string.Empty)
                                       .Trim());

// you now have "foo" and "purple", and anything else.

您可能需要调整冒号等。

Try something like this, using LINQ:

List<string> lines = new List<string>{
"Hello world",
"Description: foo",
"Garbage:baz",
"description purple"};

 //now add all your lines from your html doc.
 if (aLines[x].Contains(filterCombo.SelectedValue))
 {
       lines.Add(RemoveHTML(aLines[x]) + "\r\n");
 }

var myDescriptions = lines.Where(x=>x.ToLower().BeginsWith("description"))
                          .Select(x=> x.ToLower().Replace("description",string.Empty)
                                       .Trim());

// you now have "foo" and "purple", and anything else.

You may have to adjust for colons, etc.

无人问我粥可暖 2024-12-15 16:49:05
void Main()
{
    string test = "<html>wowzers description: none <div>description:a1fj391</div></html>";
    IEnumerable<string> results = getDescriptions(test);
    foreach (string result in results)
    {
        Console.WriteLine(result);  
    }

    //result: none
    //        a1fj391
}

static Regex MyRegex = new Regex(
      "description:\\s*(?<value>[\\d\\w]+)",
    RegexOptions.Compiled);

IEnumerable<string> getDescriptions(string html)
{
    foreach(Match match in MyRegex.Matches(html))
    {
        yield return match.Groups["value"].Value;
    }
}
void Main()
{
    string test = "<html>wowzers description: none <div>description:a1fj391</div></html>";
    IEnumerable<string> results = getDescriptions(test);
    foreach (string result in results)
    {
        Console.WriteLine(result);  
    }

    //result: none
    //        a1fj391
}

static Regex MyRegex = new Regex(
      "description:\\s*(?<value>[\\d\\w]+)",
    RegexOptions.Compiled);

IEnumerable<string> getDescriptions(string html)
{
    foreach(Match match in MyRegex.Matches(html))
    {
        yield return match.Groups["value"].Value;
    }
}
浮光之海 2024-12-15 16:49:05

改编自代码项目

string value = "ABC - UPDATED";
int index = value.IndexOf(" - UPDATED");
if (index != -1)
{
    value = value.Remove(index);
}

它将打印 ABC 没有 - 更新

Adapted From Code Project

string value = "ABC - UPDATED";
int index = value.IndexOf(" - UPDATED");
if (index != -1)
{
    value = value.Remove(index);
}

It will print ABC without - UPDATED

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文