I’m making a wordgame where I’m pulling in the Scrabble OSPD as a .txt document to check if words exist, yet I’m having trouble with equivalency checking. The dictionary is formatted like this:
var aTemp:String[];
var tWordList = Resources.Load("ScrabbleOSPD", typeof(TextAsset)) as TextAsset;
aTemp = tWordList.text.Split("\
"[0]);
With this setup, if I Debug.Log(aTemp[9]), it prints out aardvark to the console. However, when I check if(aTemp[9] == “aardvark”), it always returns False, even when I try other words. Does anyone know why this is happening? I originally thought the newline characters may have not been removed by split, but checking against “aardvark
" and "
aardvark” also both return False.
var tWordList = Resources.Load("ScrabbleOSPD", typeof(TextAsset)) as TextAsset;
var aTemp = tWordList.text.Split("\
"[0]).Select(function(s) s.Trim()).ToList();
if(aTemp.Contains("aardvark"))
{
//Do something
}
@whydoidoit This line is not working unity shows error to this line. var aTemp : String[] = tWordList.text.Split("\n"[0]); The error comes is Unexpected symbol :', expecting )', ,', ;', [', or =' What other way can i do this? Also this line also shows error? var aTemp = tWordList.text.Split("\\n"[0]).Select(function(s) s.Trim()).ToList();
I printed out each character in the word and its corresponding ASCII value:
function Start ()
{
var tWordList = Resources.Load("ScrabbleOSPD", typeof(TextAsset)) as TextAsset;
var aTemp : String[] = tWordList.text.Split("
"[0]);
var word = aTemp[9];
var message : String = "";
for(var i = 0; i < word.Length; ++i)
{
message += "{" + word _+ " : " + parseInt(word*) + "}, ";*_
} Debug.Log(message); } This is the output I’m getting: {a : 97}, {a : 97}, {r : 114}, {d : 100}, {v : 118}, {a : 97}, {r : 114}, {k : 107}, { : 13}, Notice { : 13} at the end? According to the [ASCII table][1], 13 is the code for carriage return (“\r”). You were looking for "
" only, keep in mind that some text editors add "\r
" instead of just "
", for completeness. Figure a way to split with "\r
", or use aTemp[9] == "aardvark\r" for your comparison: if(aTemp[9] == “aardvark”) Debug.Log(“A”); if(aTemp[9] == “aardvark\r”) Debug.Log(“B”);
_*[1]: http://www.asciitable.com/*_
All that said, if you were using C# you could use the generic HashSet<>, which is like a dictionary, but the keys and values are the same data. You could then just check if the word is contained in the set, which should be faster than looping through each word in an array to find the match.
Note that .Trim() will remove all such whitespace from both ends of a string and will be compatible with all platforms - many of which do not append a /r
@whydoidoit This line is not working unity shows error to this line. var aTemp : String[] = tWordList.text.Split("\n"[0]); The error comes is Unexpected symbol
– ShahanButt:', expecting)',,',;',[', or=' What other way can i do this? Also this line also shows error? var aTemp = tWordList.text.Split("\\n"[0]).Select(function(s) s.Trim()).ToList();