Splitting textasset into an array and binary search

I’m making a word game, all words are stored in a SORTED txt file in my resources folder. I’ve learned the only way to be platform independent is to use the resources.load method. However, when I split my text asset into an array I am having trouble searching the array. Here’s my code`

 TextAsset asset = (TextAsset)Resources.Load("words");
 string[] words = asset.text.Split('

Now if I print out random positions in the array the words print out as expected, so they are all properly loaded up. There are no extra characters. But for some reason when I do a binary search on it it never finds my word.

 int result = Array.BinarySearch(words, "hello");

This will return a negative value indicating it couldn’t be found, even if I print out the exact location that I know where it is in the array and it prints out “hello”. To my eye they always look identical and this is driving me nuts. Could it have something to do with text encoding? It was working no problem when I did some file IO and split the file like `

 string[] words = System.IO.File.ReadAllLines("Path");`

But if I do that I have to figure out the file path for each platform(this is mobile iOS, Android, shortly Windows phone).

I’m at a loss here…any help would be appreciated or other ideas. Thanks!


Move the TextAsset out of Resources to anywhere in Assets (including a subfolder you create). At the top of the class you will put:

public TextAsset asset;

Your script above will be attached to a game object. Select the game object in the Hierarchy. Then drag and drop the text asset on to the asset variable (where it says ‘None (TextAsset)’).

As for splitting the lines, I am inexperienced with C# text manipulation, but I would do it this way:

char[] archDelim = new char[] { '\r', '

’ };
words = asset.text.Split(archDelim, StringSplitOptions.RemoveEmptyEntries);

I think you need to do a ‘using System.Text;’ in order to get ‘StringSplitOptions’.