解析在xcode中的文本文档

时间:2012-03-17 21:59:53

标签: objective-c parsing ios5 xcode4.2

我想通过.txt文档并在NSStrings中存储文本块。我的问题是这个文档包含换行符,我不知道如何摆脱它们。如果我可以将每个单词放入有序的NSArray然后只是通过该数组并从中获取信息,那将是很好的。我会这样的:

// txt file
This is just a test.
End of the text file.

// NSArray and NSStrings
NSArray *wholeDocument =@"This","is","just","a","test","Foo","bar.", "End", "of", "the","text","file.";
NSString *beginDocument =@"This is just a test";
NSString *endDocument =@"End of the text file.";

1 个答案:

答案 0 :(得分:0)

试试这个:

NSString *str = [NSString stringWithContentsOfFile:@"file.txt"];
NSArray *arr = [str componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

此处,arr将包含由空格或换行符分隔的所有单词。