Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove words from string c#

Tags:

string

c#

asp.net

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>&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);
    }
like image 981
nGX Avatar asked Oct 06 '11 19:10

nGX


People also ask

How to delete a Word in a string?

Remove a Given Word from a String using C++ For example, we can use a simple approach to remove a word from a string. First, put the given string in 2-D matrix form, where each word is stored in each row. Find the word in the matrix and replace that row with the null character where the word.

How do I remove a Word from a string in Java?

Remove Word from String in Java using Library Functionstr = str. replaceAll(word, "");

How do I remove a Word from a sentence in Python?

Remove a Word from String using replace() print("Enter String: ", end="") text = input() print("Enter a Word to Delete: ", end="") word = input() wordlist = text. split() if word in wordlist: text = text.


2 Answers

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("&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);
}
like image 163
nGX Avatar answered Oct 09 '22 05:10

nGX


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.stack.imgur.com/rbluf.png

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

like image 41
Naveen Chandran Avatar answered Oct 09 '22 06:10

Naveen Chandran