Array.Contains() 总是错误的

Array.Contains() is always false

几天来我一直在为这个问题绞尽脑汁,并尝试了很多不同的方法。我在论坛上到处都是,尝试了我所看到的一切,但没有成功。我的问题可能是我没有覆盖,但我不知道如何让它工作。

我想检查包含 5,000 多个元素的数组是否包含用户输入的单词。这个词一个字符一个字符地输入,并组合成一个字符串(guessString)。然后我使用 .Contains() 来查看该单词是否在数组中。

***编辑请查看调试日志的屏幕截图WordArray Elements -- Debug Output -- Debug With whitespace detection -- Code that doesnt work

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Linq;


public class WordAction : MonoBehaviour
{
    TMP_Text m_textComponent;
    TMP_Text m_currentSquare;
    public TMP_Text[] squareArray;

    List<string> dupKey = new List<string>();

    public string[] WordArray;
    public List<string> DictionaryList = new List<string>();
    public TextAsset file;

    [SerializeField]
    Color correctColor, wrongColor, maybeColor;

    [SerializeField]
    float colorFadeSpeed, colorFadeTime; // 2f, 1f

    public float waitTime;
    public string levelID;
    public string key;
    public AudioSource revealSFX;

    bool guess;
    string guessString;
    int guessRegulator;
    int guessCount = 1;
    int lessGuessCount; // Starts variable at value of first current row element
    int maxGuessCount;

    string[] guessStringArray = new string[1];

    void Start()
    {

        for (int i = 0; i < 5; i++) // Duplicate key
        {
            dupKey.Add(key[i].ToString());
        }

        var content = file.text;
        string[] AllWords = content.Split('\n');
        WordArray = AllWords;
    }


    public void Magic()
    {
        StartCoroutine(CompareKey());
    }

    IEnumerator CompareKey()
    {
        guessRegulator++;
        GuessRegulatorFunction();

        lessGuessCount = (guessCount * 5) - 5; // Starts variable at value of first current row element
        maxGuessCount = guessCount * 5;
        guessCount++; // Moves to next row
        int k = 0; // Indexer for key[]
        int cW = 0; // Indexer for CombineWord()
        GameObject keyGO; // Keyboard GO


        for (int i = lessGuessCount; i < maxGuessCount; i++)
        {
            if (cW < 1)
            {
                CombineWord(i);

                cW++;
            }

            bool match = WordArray.Contains(guessString); // not working
            Debug.Log(match);
            if (match)
            {
                //do stuff
            }

            //compare stuff
            string guessStr = squareArray[i].text.ToString();
            string keyStr = key[k].ToString();
            bool result = guessStr == keyStr;

            if (!result && !dupKey.Contains(guessStr))
            {
                //wrong stuff
                GameObject parentGO = squareArray[i].transform.parent.gameObject; // Gets parent of SquareArray element
                Image parentImage = parentGO.GetComponent<Image>(); // Gets Image component of parent game object         
                keyGO = GameObject.Find(squareArray[i].text); // Keyboard
                Image keyParentImage = keyGO.GetComponent<Image>(); // Keyboard

                wrongColor.a = 255;
                keyParentImage.color = wrongColor;
                parentImage.color = wrongColor;
                yield return null;
            }

            if (result)
            {
                //correct stuff
                dupKey[k] = "";

                GameObject parentGO = squareArray[i].transform.parent.gameObject; // Gets parent of SquareArray element
                Image parentImage = parentGO.GetComponent<Image>(); // Gets Image component of parent game object         
                keyGO = GameObject.Find(squareArray[i].text); // Keyboard
                Image keyParentImage = keyGO.GetComponent<Image>(); // Keyboard

                correctColor.a = 255;
                keyParentImage.color = correctColor;
                parentImage.color = correctColor;
                yield return null;
            }

            if (!result && dupKey.Contains(guessStr))
            {
                //yellow stuff
                for (int x = 0; x < 5; x++) // Duplicate key
                {
                    if (guessStr == dupKey[x])
                    {
                        dupKey[x] = "";
                    }
                }

                GameObject parentGO = squareArray[i].transform.parent.gameObject; // Gets parent of SquareArray element
                Image parentImage = parentGO.GetComponent<Image>(); // Gets Image component of parent game object         
                keyGO = GameObject.Find(squareArray[i].text); // Keyboard
                Image keyParentImage = keyGO.GetComponent<Image>(); // Keyboard

                maybeColor.a = 255;
                keyParentImage.color = maybeColor;
                parentImage.color = maybeColor;
                yield return null;
            }

            revealSFX.Play();
            k++;
            yield return new WaitForSeconds(waitTime);
        }

        dupKey.Clear();
        for (int i = 0; i < 5; i++) // Duplicate key
        {
            dupKey.Add(key[i].ToString());
        }

    }

    void GuessRegulatorFunction()
    {
        guessRegulator++; // Stops multiple guess attempts

        for (int i = 0; i < (guessCount * 5); i++) // Checks if row is blank when guessing
        {
            if (squareArray[i].text == "")
            {
                guess = false;
                guessRegulator = 0; // Resets guess regulator
                break;
            }
            else
            {
                guess = true;

            }
        }

        if (guessRegulator > 1 || guess == false) // Stops multiple guess attempts
        {
            return;
        }
    }

    public void BackSpace()
    {
        for (int i = guessCount * 5; i > (guessCount * 5) - 6; i--)
        {
            if (squareArray[i].text != "")
            {
                squareArray[i].text = "";
                break;
            }
        }
    }

    public void InputLetter()
    {
        guessRegulator = 0;

        for (int i = 0; i < guessCount * 5; i++)
        {
            if (squareArray[i].text == "")
            {
                squareArray[i].text = EventSystem.current.currentSelectedGameObject.name.ToString();
                break;
            }
        }
    }

    void CombineWord(int i)
    {
        var string1 = squareArray[i].text.ToString();
        var string2 = squareArray[i + 1].text.ToString();
        var string3 = squareArray[i + 2].text.ToString();
        var string4 = squareArray[i + 3].text.ToString();
        var string5 = squareArray[i + 4].text.ToString();

        guessString = string1 + string2 + string3 + string4 + string5;
        //Debug.Log(guessString);
    }
}

嗯,你的 WordArray 总是空的。您将文件内容放入名为 AllWords.

local 变量中

永远不会在空数组中找到您的单词。

我已经把你那行不起作用的代码一字不差地复制了下来。然后,我获取了您所说的 WordArrayguessString 变量中的数据并进行了设置。然后我运行这个:

var WordArray = new [] { "WHICH", "THERE", "THEIR", "ABOUT" };
var guessString= "THERE";
bool match = WordArray.Contains(guessString);
Console.WriteLine(match);

match出来True.

您的变量不包含您认为的数据。

很可能您调用 .Split('\n')content 实际上包含 Windows 行尾标记,因此是 "\r\n" 的组合。由于您仅在 '\n' 上拆分,因此 '\r' 可能保留在您的字符串中,因此 "THERE""THERE\r".

不匹配

试试这个:

.Split(new[] { Environment.NewLine }, StringSplitOptions.None);

如果您的代码在 Windows 或 Linux 上是 运行,则以上行有效。请注意混合结尾的文件。