- Home /
How do I find a String in an Array?
humanReadableWord is declared at the top of the file and works. I can print it and I get a string. I'm able to print the array with allWords just fine. I can pull from the array. I can print this.humanReadableWord with no problem but I can't compair them for some reason... I think.
var humanReadableWord : String;
function Update(){
var selectedLetters = GameObject.Find("SelectedLetters").GetComponent(SelectedLetters);
var currentWordInProgress = selectedLetters.selectedLettersArray;
this.humanReadableWord = (currentWordInProgress).Join("");
}
function ValidateWord(){
var linkToDictionary = GameObject.Find("Dictionary").GetComponent(Dictionary);
var allWords = linkToDictionary.dictionaryWords;
var currentWord = ""+this.humanReadableWord;
for (var i = 0; i < allWords.length; i++) {
// print(this.humanReadableWord); <--Works
if(allWords[i] == currentWord){
print("testing!!");
}
}
}
I have a call to the function below this that I also know is working...
You might also like this: http://msdn.microsoft.com/en-us/library/xfhwa508(v=vs.80).aspx
Original post edited. I'm still working on this and I'm wondering if I'm missing something here.
" if(allWords[i] == currentWord){ ""
Answer by Eric5h5 · Dec 18, 2012 at 01:30 AM
Use Array.Contains; you don't need to loop through the array yourself. Also I don't know what "var currentWord = ""+this.humanReadableWord;" is for since it's not doing anything...you can just use the humanReadableWord variable instead.
I tried using the Array.Contians but I always get 'Contains' is not a member of 'Array'.
You're not using the JS Array class are you? Always use built-in arrays or generic List ins$$anonymous$$d.
I'm very new to program$$anonymous$$g but very dedicated. All day I've been reading about arrays and what types are what. This is what i did to create my Array. I get the results from it but I can't search it. I tried using the builtin but it acts like it doesn't recognize the builtin.
This is what I have.
var dictionaryWords = new Array(); var dictionaryTextFile : TextAsset;
function Start () {
var returnChar = "\n"[0]; this.dictionaryWords = this.dictionaryTextFile.text.Split(returnChar); }
O$$anonymous$$, you should do this ins$$anonymous$$d:
var dictionaryWords : String[];
Was never able to get the Application to be ok with Array.Contains. Unity gripes every time. So I would still love to resolve that but now I have a new issue and maybe it's too big for this thread. So I might start a new one.
It turns out that the Array that I'm building is tacking on an extra space at the end of each word. So for instance when I run length on the word in the array and the word I'm using, I get the following.
word in array "aah" // length = 4 word user is typing "aah" // length = 3
That's why the match never happens but now I'm blown away as to why it's saying my string is one letter too long. I patched a band-aid on it by chopping it one char shorter and its working fine but it's a kluge to be sure...
Thoughts?
Answer by blkbear · Dec 18, 2012 at 02:30 AM
first try printing out the variable currentWord. And if the log is what you want then maybe the array value at i is not a match. You could also try using === which implies strictly equal