0

Is it possible to remove "and","is","and other words" instead of just "and"

original = original.Replace("and", String.Empty);

I am trying to create a program that can detect and kind of compress a text file down by getting rid of irrelevant words like "and" , "is", "because" etc etc.

string original = "yeet is and this because working is  what help me.";

original = original.Replace("and", String.Empty);
Console.WriteLine(original);
Console.ReadLine();

It wont run the code instead it will just say "Name can be simplified" if I do "and","in" I did try using or as in ||.

  • 1
    You can just keep doing `.Replace().Replace().Replace()` to do multiple or switch over to something more flexible like regex. [here's more info](https://learn.microsoft.com/en-us/dotnet/api/system.string.replace?view=netframework-4.8) Example from that page: `s = s.Replace('a', 'b').Replace('b', 'c').Replace('c', 'd');` – JNevill Jul 29 '19 at 16:50
  • "Name can be simplified" is just a suggestion. It shouldn't have any bearing on your ability to run the code or what it does. And it can't be referring to any of the lines of code you've shown us. Are you seeing an error in the Errors pane? Does a console open? Please tell us in more detail what is _actually_ happening. – JLRishe Jul 29 '19 at 16:51
  • 1
    Possible duplicate of [Replace Multiple String Elements in C#](https://stackoverflow.com/questions/1321331/replace-multiple-string-elements-in-c-sharp) – KevinLamb Jul 29 '19 at 16:52

5 Answers5

0

If you have a bunch of "irrelevant" words, you can store them in a list and then replace them in a loop:

var irrelevantWords = new List<string>
{
    "and", "is", "the", "of"
};

string original = "yeet is and this because working is  what help me.";

foreach (var irrelevantWord in irrelevantWords)
{
    original = original.Replace(irrelevantWord, string.Empty);
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43
0

Is there any reason you can't do this?

original = original.Replace("and", String.Empty);
original = original.Replace("because", String.Empty);
original = original.Replace("is", String.Empty);          
Console.WriteLine(original);
Console.ReadLine();---

If you look at the String.Replace Method, you'll see that it can only accept 2 arguments at a time, either char,char or string,string.

Brando
  • 1
0

using String.Replace, I don't think so. See Replace Multiple String Elements in C#

However, you could use Join and Split. Something like:

var exceptWords = new HashSet<string>
{
    "and", "is", "the", "of"
};

string original = "yeet is and this because working is  what help me.";
var originalSet = new HashSet<string>(original.Split(' '));
originalSet.ExceptWith(exceptWords);
var output = String.Join(" ", originalSet);
Mhd
  • 2,778
  • 5
  • 22
  • 59
0

I'm going to throw my hat in on this one, too. I just tested both of these methods and they now work correctly (after edits), without removing parts of words as well as keeping duplicate words. Since there's no sorting, it also keeps the order of the words. They also remove accidental extra white spaces between words.

var irrelevantWords = new List<string>
{
    "and", "is", "the", "of"
};

string original = "yeet is and this because working is  what help me.";
List<string> result = new List<string>();

foreach (string word in original.Split(' '))
{
    if (!word.Equals(string.Empty) && (irrelevantWords.IndexOf(word) == -1))
    {
        result.Add(word);
    }
}
return string.Join(" ", result);

The loop can be shortened to a single line with Linq.

return string.Join(" ", original
    .Split(' ')
    .ToList()
    .Where(word => !word.Equals(string.Empty) && (irrelevantWords.IndexOf(word) == -1))));
computercarguy
  • 2,173
  • 1
  • 13
  • 27
-1

Similar to @Rufus L, you could define a custom extension method like shown below:

public static class StringExtension {
    public static string ReplaceMultiple(this string s, string[] replace) {
        foreach (string r in replace) {
            s = s.Replace(r, String.Empty);
        }

        return s;
    }
}

Then you can use it like follows:

string original = "yeet is and this because working is  what help me.";
original = original.ReplaceMultiple(new string[] { "and", "is" });
Console.WriteLine(original);

This works and the output is:

yeet th because working what help me.

ivcubr
  • 1,988
  • 9
  • 20
  • 28