将 .txt 文件分成单独的字符串

发布于 2024-12-19 16:15:53 字数 408 浏览 2 评论 0原文

我已经知道如何上传文件并将其内容放入字符串中:

NSString *fileContents = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"WellBalanced Recipes" ofType:@"txt"] 
                                                   encoding:NSUTF8StringEncoding 
                                                      error:NULL];

但是,我想将此文本文档解析为单独的字符串。例如,如果我在同一个文档中有不同的食谱,我想以某种方式分离每个食谱。

I already know how to upload a file and place its contents into a string with:

NSString *fileContents = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"WellBalanced Recipes" ofType:@"txt"] 
                                                   encoding:NSUTF8StringEncoding 
                                                      error:NULL];

However, I would like to parse this text document into separate strings. For example, if I had different recipes in the same document, I would like to somehow separate each recipe.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞陪衬 2024-12-26 16:15:53

使用 - (NSArray *)componentsSeparatedByString:(NSString *)separator

NSArray *data = [fileContents componentsSeparatedByString:@"\n"];

Use - (NSArray *)componentsSeparatedByString:(NSString *)separator

NSArray *data = [fileContents componentsSeparatedByString:@"\n"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文