- Home /
Reading line-wise?
Well i got the TextAsset implemented, but if i f.e print it it dashes out the complete file...i want to read it linewise and save each line for later use.
Using Javascript.
Answer by Eric5h5 · May 12, 2011 at 12:40 PM
You can use String.Split on line breaks, to make a String[] array, where each entry in the array is a line of text from the TextAsset:
var lines = myTextAsset.text.Split("\n"[0]);
Erik's answer will work just fine indeed. :) If you want additional speed, by the way, it's also possible to stream-read the file using a TextReader. It has a ReadLine-method, see this article: http://msdn.microsoft.com/en-us/library/system.io.textreader.readline.aspx
A stream has the added advantage that it doesn't load the entire file into memory and run through everything in one go to split it, but does it on the fly ins$$anonymous$$d. This isn't usually a problem with textfiles, though.
As for splitting a String[], yes, just use String.Split on each line as necessary. var words = lines[0].Split(" "[0]);
for example would split on the spaces in lines[0]. Use a loop to go through all lines.
Thanks for all the nice answers. But now my problem is to use the Split function on a String[]
thx i will try that, eric. also, we share the same first name from what it looks like :P
Your answer
