0

我怎样才能做到这一点,以便我可以将从文件中读取的字符串拆分为单独的字符串,或者让它只读取特定的行,就像我希望它只读取文本文件的第一行并将其存储到字符串中一样。这是我用来读取文本文件的代码。

    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
4

1 回答 1

2

读取字符串并将其拆分

NSString *str = [NSString stringWithContentsOfFile:<YOUR FILEPATH> encoding:NSUTF8StringEncoding error:nil];
NSArray *array = [str componentsSeparatedByString:@"\n"];

(例如阅读第一行。)

NSString *firstline = [array objectAtIndex:0];
于 2013-04-09T23:52:16.353 回答