Reputation: 11
How could I make it so that I can split up a string read from a file into separate strings or make it only read certain line like if i wanted it to only read the first line one the text file and store it into a string. Here is the code i use to read a text file.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pday11 = [documentsDirectory stringByAppendingPathComponent:@"day11.txt"];
NSString *stringFromFileday11 = [NSString stringWithContentsOfFile:pday11 encoding:NSUTF8StringEncoding error:nil];
//If i used this to read the file how could i split up the "stringFromFileday11" into different strings where every line is a string from the file
Upvotes: 0
Views: 150
Reputation: 121
Read to a string and split it up
NSString *str = [NSString stringWithContentsOfFile:<YOUR FILEPATH> encoding:NSUTF8StringEncoding error:nil];
NSArray *array = [str componentsSeparatedByString:@"\n"];
(e.g. Read a first line.)
NSString *firstline = [array objectAtIndex:0];
Upvotes: 2