尝试在另一个字符串中查找字符串数组中包含的任何字符串时出现问题

Issue when trying to look for any of the strings contained within a string array inside another string

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Reflection;
using Microsoft.Office.Interop;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.VisualBasic;

namespace CheckList
{

public partial class AdHocChecker : Form
{
    // declaring variables
    private string _file = @"D:\test.xlsm";
    Excel.Workbook wkb;
    Excel.Worksheet sheet;

    private int _columnToCheck;
    private string _columnText;
    private int _rowToCheck;
    private int _rowToWrite;

    // array that will hold several string elements
    private string[] _stringsArray = new string[4];
    private string _str;

    Excel.Application excelApp = new Excel.Application();



    public AdHocChecker()
    {
        InitializeComponent();

        _columnToCheck = 5;
        _rowToCheck = 2; // starting from two because 1 would be the title of that column
        _rowToWrite = 3;

        _stringsArray[0] = "Chocolate";
        _stringsArray[1] = "Bananas";
        _stringsArray[2] = "Strawberries";
        _stringsArray[3] = "Peaches";           

        // setting Excel up and opening workbook
        excelApp.Visible = true;
        wkb = excelApp.Workbooks.Open(_file,  0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",     
        true, false, 0, true, false, false);

        // letting program know that this workbook's current Active Sheet is the one we're going to work with
        sheet = wkb.ActiveSheet;
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        // looping through sheet's rows
        for (int _rowToCheck = 2; _rowToCheck < sheet.Rows.Count; _rowToCheck++)
        {
            // take the current string to check from column E (_columnToCheck)
            _columnText = sheet.Cells[_rowToCheck, _columnToCheck].Value;
            Console.WriteLine("RECIPE: " + _columnText);

            // we loop inside of loop (to check for any of the strings contained in the array)
            for (int i = 0; i < _stringsArray.Length; i++)
            {
                _str = _stringsArray[i];

                Console.WriteLine("STRING: " + _str);

               /* if (_columnText.IndexOf(_str) == 1)
                {
                    // POSITIVE RESULT
                    sheet.Cells[_rowToCheck, _rowToWrite] = "YES";
                }
                else
                {
                    // NEGATIVE RESULT
                    sheet.Cells[_rowToCheck, _rowToWrite] = "NO";
                }*/

                if (_columnText.Contains(_str))
                {
                    // POSITIVE RESULT
                    sheet.Cells[_rowToCheck, _rowToWrite] = "YES";
                }
                else if (!_columnText.Contains(_str))
                {
                    // NEGATIVE RESULT
                    sheet.Cells[_rowToCheck, _rowToWrite] = "NO";
                }
            }
        }        
    }
}

此程序会打开一个 excel 电子表格并循环查看一列的行以检查配方。然后它循环遍历一组成分,如果在存储配方的字符串中找到任何成分,它应该将正值 (YES) 或负值 (NO) 写入另一个单元格。

我尝试同时使用 Contains 和 IndexOf(正如您在注释掉的部分中看到的那样),但它总是将 "NO" 写入每一行,而不考虑是否有任何成分在字符串配方中与否。

这是输出:

RECIPE: Coconut Lettuce Pineapple Sesame
STRING: Chocolate
STRING: Bananas
STRING: Strawberries
STRING: Peaches
RECIPE: Chocolate Sugar Dough Pineapple
STRING: Chocolate
STRING: Bananas
STRING: Strawberries
STRING: Peaches
RECIPE: Mint Bananas Milk Dough
STRING: Chocolate
STRING: Bananas
STRING: Strawberries
STRING: Peaches

根据该输出,第一行应为负值,但第二和第三个配方应为正值。输出让我认为循环很好,问题出在 Contains 或 IndexOf 的使用上,但对我来说似乎没问题。有人知道这里有什么问题吗?

祝你有美好的一天, P.

好的,我忘记休息了;当匹配为正时:)

修改后效果很好:

            if (_columnText.Contains(_str))
            {
                // POSITIVE RESULT
                sheet.Cells[_rowToCheck, _rowToWrite] = "YES";
                break;
            }
            else if (!_columnText.Contains(_str))
            {
                // NEGATIVE RESULT
                sheet.Cells[_rowToCheck, _rowToWrite] = "NO";
            }