C# 如何检查列表中是否存在特定字符串<;字符串>;如果不从列表中删除索引?

C# 如何检查列表中是否存在特定字符串<;字符串>;如果不从列表中删除索引?,c#,.net,winforms,C#,.net,Winforms,我试过了,但没用。 我正在获取索引越界异常 for (int x = 0; x < newText.Count; x++) { for (int y = 0; y < WordsList.words.Length; y++) { if (!newText[x].Contains(WordsList.words[y])) { for (int n = 0; n < 3; n++)

我试过了,但没用。 我正在获取索引越界异常

for (int x = 0; x < newText.Count; x++)
{
    for (int y = 0; y < WordsList.words.Length; y++)
    {
        if (!newText[x].Contains(WordsList.words[y]))
        {
            for (int n = 0; n < 3; n++)
                newText.RemoveAt(x);
        }
    }
}
等等

我想做的是循环新文本,如果在索引0中没有任何单词(字符串),那么删除索引0,1,2下一个i插入检查索引3中的任何单词,如果不存在,则删除索引3,4,5

如果在索引0或3中有一个或多个单词,则不执行任何操作,也不删除任何内容。 最后,新文本的格式应与以前相同:

index 0 text line
index 1 date&time
index 2 empty ""
新的newText内容将包含包含单词中一个或多个字符串的文本行

编辑

这就是我现在尝试的: 首先,这是我构建列表的方式:

List<string> t = filterNumbers(text);
            for (int i = 0; i < t.Count; i++)
            {
                if (!newText.Contains(t[i]))
                {
                    newText.Add(t[i]);
                    newText.Add(dateTime[i]);
                    newText.Add("");
                }
            }
索引超出范围。必须为非负数且小于集合的大小

编辑

查看您的逻辑:

(1)    for (int i = newText.Count - 1; i >= 0; i--)
       {
(2)        for (int x = 0; x < WordsList.words.Length; x++)
           {
               if (...)
               {
(3)                 newText.RemoveAt(i);
               }
           }
       }
(1)for(int i=newText.Count-1;i>=0;i--)
{
(2) for(int x=0;x
您可以看到,即使删除了(3)中的行,也可以继续在(2)中循环,这样就可以再次尝试删除(2)中的行以获得新的索引,而新索引现在已超出范围


您需要在(3)之后添加
break
,以继续循环(1)

如果我正确理解您想要检查特定行是否包含一些单词,如果没有,请删除该行和以下两行,下面是一个可能的解决方案:

// start at the bottom in the first line "that matters" and go down by 3
for (int x = newText.Count - 3; x >= 0; x-=3) 
{
    // check if the line contains any of the words specified
    if (!WordsList.words.Any(w => newText[x].Contains(w)) || newText[x] == "") 
    {
        // remove the checked line as well as the next two if not
        l.RemoveRange(x, 3); 
    }
}
编辑 将谓词更正为:

!WordsList.words.Any(w => newText[x].Contains(w));

编辑2 添加了空字符串作为可能


问题是,如果要测试的行为空,它将通过测试,因为它不包含
WordsList.words
中的任何单词。测试现在将空字符串作为一个选项,并在遇到时将其删除。

好的,您的数据结构似乎非常糟糕。无论如何,如果你必须保持结构不变,我认为这是可行的:

var  newList = new List<string>();
for (int index = 0; index < newText.Count; index = index + 3)
{
    if (WordsList.Any(t => newText[index].ToLower().Trim().Contains(t.ToLower().Trim())))
    {
        newList.AddRange(newText.Skip(index).Take(3));
    }
}

newText = newList; 
var newList=newList();
for(int index=0;indexnewText[index].ToLower().Trim().Contains(t.ToLower().Trim()))
{
AddRange(newText.Skip(index.Take)(3));
}
}
newText=newList;
//对于每个3字集
对于(int x=0;x

我只是想用你的原始代码进行测试和实验。我还没有测试过这个。只需确保列表中包含3×n个项目。

您在
newText
上进行的
RemoveAt
调用将删除
i
处的元素。然后下次它在
x
上循环时,你就出界了。你编辑的答案的逻辑对于你最初的问题是完全有争议的。如果进行编译,您将面临一个问题,即删除的行数将是最初计划的两倍多,因为您不仅检查“重要”行,而是检查导致100多行删除的每一行,因为字符串
”,在代码中出现50次,不包含任何单词,将被删除。这仍然不会按照作者的意愿删除3行使用
newText.AddRange(index,3)
以获得更清晰的信息相同此解决方案:此解决方案返回空列表。它删除所有索引/行。我检查了至少一个我在网站上看到的单词也存在于单词列表中。我认为单词列表的字符串。单词与newText第一个索引中的字符串不完全相同。也许它是区分大小写、空格或编码的东西。尝试使用较低的修剪。我用它编辑了我的解决方案。也许你是对的。字符串/单词都是希伯来语,新文本中的文本也是希伯来语,所以可能需要编码?我知道在form1构造函数中,我第一次下载网站的html文件时需要对其进行编码:client.Encoding=System.Text.Encoding.GetEncoding(1255);所以也许这里也一定有重唱?重唱对我来说很痛苦。你能在下载的html字符串上试试这个吗?byte[]bytes=Encoding.GetEncoding(1255).GetBytes(下载的htmlstring);var newString=Encoding.Default.GetString(字节);并告诉我该解决方案是否有效返回空列表。它删除所有索引/行。我检查了至少一个我在网站上看到的单词,也在单词列表中。@user3739928我的错,我犯了一个大错误:D现在应该更正!三个FX的工作几乎很好。过滤是可以的,但最后我看到3个空格和一条日期和时间线。最后一行是日期和时间,这没问题,但是在它之后有三行空行,在日期和时间之后还有一行。最后应该只有日期和时间线。ThreeFx我已经更新了我的问题,现在我添加了一个屏幕截图,并用黑色方框标记了问题所在的区域。您可以选择最后一行是日期和时间,但在它之后有更多的2-3个空行,在它们之后还有另一个日期和时间行。格式应该始终是文本行的第一个索引,最后一个日期和时间。@user3739928修复了它,问题是空字符串
// start at the bottom in the first line "that matters" and go down by 3
for (int x = newText.Count - 3; x >= 0; x-=3) 
{
    // check if the line contains any of the words specified
    if (!WordsList.words.Any(w => newText[x].Contains(w)) || newText[x] == "") 
    {
        // remove the checked line as well as the next two if not
        l.RemoveRange(x, 3); 
    }
}
!WordsList.words.Any(w => newText[x].Contains(w));
!WordsList.words.Any(w => newText.Contains(w));
var  newList = new List<string>();
for (int index = 0; index < newText.Count; index = index + 3)
{
    if (WordsList.Any(t => newText[index].ToLower().Trim().Contains(t.ToLower().Trim())))
    {
        newList.AddRange(newText.Skip(index).Take(3));
    }
}

newText = newList; 
// For each 3-word-sets
for (int x = 0; x < newText.Count; x += 3)
{
   // For each word in that 3-word-set
   for (int k = x; k < 3; k++)
   {
      // Check each word
      bool breakOut = false;
      for (int y = 0; y < WordsList.words.Length; y++)
      {
         if (!newText[k].Contains(WordsList.words[y]))
         {
            newText.RemoveAt(x+2);
            newText.RemoveAt(x+1);
            newText.RemoveAt(x);
            x -= 3;
            breakOut = true;
            break;
         }
      }
      if (breakOut) { break; }
   }
}