【发布时间】:2016-09-09 00:27:25
【问题描述】:
在 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" };
【问题讨论】:
-
这是一个令人困惑的问题。您的预期结果究竟是什么?
-
@Kinetic 你好,已编辑,一切都在上面显示
-
我在您完成编辑后写了我的评论。
-
所以基本上你在一个字符串中有一些文本,在一个数组中有一堆单词。您的输出应该是文本中数组中的单词?
-
@Kinetic 输出与给定代码是
Input: "You cannot successfully determine beforehand which side of the bread to butter" Output: "You cannot beforehand which side of butter"也显示输入。期望的结果是如果我可以只保留例外而不用相反的话改变列表
标签: c# multiple-value