2012-03-17 109 views
0

我想通過.txt文檔並在NSStrings中存儲文本塊。我的問題是,這個文件包含換行符,我不知道如何擺脫這些。這將是很好的,如果我可以把每個單詞放入一個有序的NSArray中,然後通過該數組並從中獲取信息。我想是這樣的:xcode中的txt文檔解析器

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

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

回答

0

試試這個:

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

這裏,arr將包含所有這些無論是空格或換行分隔的話。