【问题标题】:Issue when trying to look for any of the strings contained within a string array inside another string尝试在另一个字符串中查找字符串数组中包含的任何字符串时出现问题
【发布时间】:2015-10-05 22:58:27
【问题描述】:
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.

【问题讨论】:

    标签: string excel loops contains


    【解决方案1】:

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

    修改后效果很好:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-26
      • 2014-11-10
      • 2022-12-14
      • 1970-01-01
      • 1970-01-01
      • 2018-05-28
      • 2012-02-18
      • 2017-06-16
      相关资源
      最近更新 更多