I am likely missing something silly.
I need to check if a string is contained within a text file, as the whole word.
I have set the text file to a list and tested the list (debug the list) and the correct word comes up. I have wrote my own text file. When I check the string (user input, which calls the method from an other script, sending through a string) against the list it does not work. if I code in a word which is in the list it also does not work. If I hard code to add a word to the list it will then work. I am lost as every bit works just not when put together. I have tried uppercase/lowercase. I have also tried both list and arrays.
Thank you in advance for any help
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class WordsList : MonoBehaviour
{
// each word
private List<string> _wordList = new List<string>();
// the text file to import from
[SerializeField]
private TextAsset _wordListTextFile;
// one long string for every word
private string _wordListFileWholeThing;
private string[] _testWordArray;
// bool to test
[SerializeField]
private bool _correct;
void Start()
{
// create one long string
_wordListFileWholeThing = _wordListTextFile.ToString();
// split the string into actual words by line break and add to list
_wordList.AddRange(_wordListFileWholeThing.Split("\n"[0]));
//TEST WORD
// _wordList.Add("BALE"); // manually adding a test word to the list
_testWordArray = new string[_wordList.Capacity];
for (int i = 0; i < _wordList.Capacity; i++)
{
string test = _wordList.ToString();
_testWordArray = test;
}
Debug.Log(_wordList[0]);
Debug.Log(_wordList[4]);
Debug.Log(_wordList[12]);
Debug.Log(_wordList[100]);
Debug.Log(_wordList.Count);
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.L))
{
// test word checker
_correct = CheckWord(_wordList[12]); // this works
}
if (Input.GetKeyDown(KeyCode.K)) // this does not work unless I manually add it to the list
{
// test word checker
_correct = CheckWordArray("BALE");
}
}
public bool CheckWordArray(string word)
{
// bool to check if word is found
bool wordFound = false;
Debug.Log("Word to look up +" + word + "+");
// loop through the list of words
for(int i = 0; i< _testWordArray.Length; i++)
{
// check to see if words equal
if(Equals(_testWordArray, word))
{
Debug.Log("word found in array");
}
}
{
}
// after loop if word is found return true
if (wordFound == true)
{
return true;
}
// if word not found return false
else
{
return false;
}
}
public bool CheckWord(string word)
{
// bool to check if word is found
bool wordFound = false;
// loop through the list of words
foreach (string str in _wordList)
{
// only check if word not already found
if (wordFound == false)
{
str.Trim();
str.TrimEnd();
str.TrimStart();
//if word is found set bool true
if (Equals(str, word))
{
Debug.Log("Word found in fucking list!");
// set the bool true if word is found
wordFound = true;
// break out of loop as no need to run anyfurther
// break;
}
}
}
// after loop if word is found return true
if (wordFound == true)
{
return true;
}
// if word not found return false
else
{
return false;
}
}
}