如何删除 if 语句的具有多个值的字符串中的异常除外
How to remove all except exception in string with multiple value for if-statement
在这个 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" };
所以这段代码:
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");
将给出此输出:
- 输入:你无法成功地预先确定面包的哪一面涂黄油
- 输出:成功判断面包到
在这个 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" };
所以这段代码:
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");
将给出此输出:
- 输入:你无法成功地预先确定面包的哪一面涂黄油
- 输出:成功判断面包到