简体   繁体   English

如何删除if语句具有多个值的字符串中的所有异常(例外)

[英]How to remove all except exception in string with multiple value for if-statement

in this multiple value exception for if-statement, I accept the condition if any value from my list is exist in the given string and then I remove this values from string: 在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();
        }
    }
}

My question is how in this code keep exceptions and remove everything else, except this words. 我的问题是这段代码中如何保留异常并删除除此词之外的所有其他内容。 So actual result is: 因此实际结果是:

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

but what can I do if with using of this same list var valueList3 = new List<string> { "successfully", "determine", "the", "bread", "to" }; 但是如果使用相同的列表,该怎么办var valueList3 = new List<string> { "successfully", "determine", "the", "bread", "to" }; I want get this result. 我想得到这个结果。

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

Correct to say I want both for same statement: 正确地说我要两个都使用同一条语句:

 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"

And of course I'm not asking for this: 当然,我不是在问这个:

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

but with same list: 但具有相同的列表:

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

So this code : 所以这段代码:

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"); 

Will give this output : 将给出此输出:

  • Input: You cannot successfully determine beforehand which side of the bread to butter 输入:您无法预先成功确定要在面包的哪一侧涂黄油
  • Output: successfully determine the bread to 输出:成功确定面包

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM