从字符串c#中删除单词

时间:2011-10-06 19:41:16

标签: c# asp.net string

我正在开发一个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>&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);
    }

5 个答案:

答案 0 :(得分:6)

好的,所以我想出了如何通过我现有的一个功能删除单词:

public static string RemoveHTML(string text)
{
    text = text.Replace("&nbsp;", " ").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);
}

答案 1 :(得分:2)

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

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

输出: 他在开车。

注意:在第二个替换其双精度空格。

链接:https://i.stack.imgur.com/rbluf.png

尝试此操作。它将删除您要删除的所有单词。

答案 2 :(得分:0)

使用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.

您可能需要调整冒号等。

答案 3 :(得分:0)

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

答案 4 :(得分:0)

Adapted From Code Project

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

如果没有ABC

,它将打印- UPDATED