从c#字符串中删除单词

本文关键字:删除 单词 字符串 | 更新日期: 2023-09-27 18:10:41

我正在做一个ASP。. NET 4.0 web应用程序,它要做的主要目标是去到MyURL变量中的URL,然后从上到下读取它,搜索所有以"description"开头的行,只保留那些,同时删除所有HTML标签。接下来我要做的是从结果后记中删除"description"文本,所以我只剩下我的设备名称。我该怎么做呢?

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

从c#字符串中删除单词

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

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

试试这个。

试试这样做,使用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.

你可能需要调整冒号等

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

改编自Code Project

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

将打印ABC而不打印- UPDATED