Remove words in string from words in array with c#

arrays, c#, string

Solution

private static List<string> wordsToRemove =
    "DE DA DAS DO DOS AN NAS NO NOS EM E A AS O OS AO AOS P LDA AND".Split(' ').ToList();

public static string StringWordsRemove(string stringToClean)
{
    return string.Join(" ", stringToClean.Split(' ').Except(wordsToRemove));
}

Modification to handle punctuations:

public static string StringWordsRemove(string stringToClean)
{
    // Define how to tokenize the input string, i.e. space only or punctuations also
    return string.Join(" ", stringToClean
        .Split(new[] { ' ', ',', '.', '?', '!' }, StringSplitOptions.RemoveEmptyEntries)
        .Except(wordsToRemove));
}

Problem

I need to remove words from a string based on a set of words: Words I want to remove: ``` DE DA DAS DO DOS AN NAS NO NOS EM E A AS O OS AO AOS P LDA AND ``` If I receive a string like: EDIT: This string is already "cleaned" from any symbols ``` THIS IS AN AMAZING WEBSITE AND LAYOUT ``` The result should be: ``` THIS IS AMAZING WEBSITE LAYOUT ``` So far I have: ``` public static string StringWordsRemove(string stringToClean, string wordsToRemove) { string[] splitWords = wordsToRemove.Split(new Char[] { ' ' }); string pattern = ""; foreach (string word in splitWords) { pattern = @"\b" + word + "\b"; stringToClean = Regex.Replace(stringToClean, pattern, ""); } return stringToClean; } ``` But it's not removing the words, any idea? I don't know if I'm using the most eficient way to do it, maybe put the words in a array just to avoid spliting them all the time? Thanks

Original source

Related problems