How to remove the letters from word1 if they exist in word2 with C#

1 Answer

0 votes
using System;
using System.Linq;

class Program
{
    static void Main()
    {
        string word1 = "forest";
        string word2 = "tor";

        // Remove characters from word1 that exist in word2
        string result = new string(word1.Where(ch => !word2.Contains(ch)).ToArray());

        Console.WriteLine($"Result: {result}");
    }
}



/*
run:

Result: fes

*/

 



answered Jul 8, 2025 by avibootz
...