如何删除所有例外的字符串与多个值的if语句

本文关键字:字符串 语句 if 何删除 删除 | 更新日期: 2023-09-27 18:06:28

在这个if语句的多值异常中,如果列表中的任何值存在于给定的字符串中,我接受这个条件,然后我从字符串中删除这些值:

using System;
using System.Collections.Generic;
using System.Linq;
namespace _01_WORKDOC
{
    class Program
    {
        static void Main(string[] args)
        {
            string searchin = "You cannot successfully determine beforehand which side of the bread to butter"; 
            var valueList3 = new List<string> { "successfully", "determine", "bread", "the", "to" }; 
            if (valueList3.Any(searchin.Contains)) 
            {
                string exceptions3 = "successfully determine bread the to"; 
                string[] exceptionsList3 = exceptions3.Split(' ');
                string test3 = searchin;
                string[] wordList3 = test3.Split(' ');
                string outtxt = null;
                var text = wordList3.Except(exceptionsList3).ToArray();
                outtxt = String.Join(" ", text);
                Console.WriteLine("Input: " + searchin + "'nOutput: " + outtxt + "'n");              
            }
            Console.Read();
        }
    }
}

我的问题是如何在这段代码中保留异常并删除除这些单词之外的所有其他内容。所以实际结果是:

Input: "You cannot successfully determine beforehand which side of the bread to butter"
Output: "You cannot beforehand which side of butter"

,但我能做什么,如果使用相同的列表var valueList3 = new List<string> { "successfully", "determine", "the", "bread", "to" };,我想得到这个结果。

Input: "You cannot successfully determine beforehand which side of the bread to butter"
Output: "successfully determine the bread to"

正确的说法是:

 Input: "You cannot successfully determine beforehand which side of the bread to butter"
 Output A: "You cannot beforehand which side of butter"
 Output B: "successfully determine the bread to"

当然我不是在要求这个:

var valueList3 = new List<string> { "You", "cannot", "beforehand", "which", "side", "of", "butter" }; 

,但有相同的列表:

  var valueList3 = new List<string> { "successfully", "determine", "the", "bread", "to" };

如何删除所有例外的字符串与多个值的if语句

那么下面的代码:

string text = "You cannot successfully determine beforehand which side of the bread to butter"; 
var words = new List<string>{ "successfully", "determine", "the", "bread", "to" };
var foundWords = string.Join(" ", words.Where(word => text.Contains(word)));
Console.WriteLine("Input: " + text + "'nOutput: " + foundWords + "'n"); 

将给出如下输出:

  • 输入:您无法事先成功确定面包的哪一面要涂黄油
  • 输出:成功确定面包到