【问题标题】:How to remove all except exception in string with multiple value for if-statement如何删除字符串中除异常之外的所有 if 语句的多个值
【发布时间】: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&lt;string&gt; { "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


【解决方案1】:

所以这段代码:

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

会给出这个输出:

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

【讨论】:

  • 正是我需要的,非常有用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-08
  • 2020-02-01
  • 1970-01-01
  • 2011-06-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多