NSString 在 NSString 中查找一个单词和下一个单词
我有一个 NSString *paragraph;
,其中填充了 N 个单词。在那里我保证有:
...
名字: 大卫
姓:约翰逊
...
我需要做的就是从这根绳子里找出大卫和约翰逊。但是我事先不知道名字是什么(David 可能是 John、Chris 等)。
在 C# 中,我只是找到以 First Name: 开头的行,然后用“”分隔单词作为分隔符..但我不知道如何在 Objective-C 中执行此操作
任何帮助将不胜感激!
I've got a NSString *paragraph;
which is filled with N number of words. In there I'm guaranteed to have:
...
First Name: David
Last Name: Johnson
...
What I need to do is find David and Johnson out of this string. However I don't know what the name is ahead of time (David could be John, Chris, etc..)
In C# I'd just find the line starting with First Name: and then separate the words with a " " as a delimiter.. But I'm lost on how to do this in Objective-C
Any help would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会使用
NSScanner
。像这样:I would use a
NSScanner
. Like this:首先使用
componentsSeparatedByString
根据行拆分字符串然后您可以过滤出以名字开头的行
然后您可以根据空格拆分并查找“Name:”标记后面的单词。
First split the string based on lines using
componentsSeparatedByString
Then you can filter out which lines start with First Name
Then you can split based on spaces and look for the word following the "Name:" token.