NSString 在 NSString 中查找一个单词和下一个单词

发布于 2025-01-05 21:08:06 字数 326 浏览 3 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(2

不必你懂 2025-01-12 21:08:06

我会使用 NSScanner。像这样:

NSString *lastName;
NSString *firstName;
NSString *text = @"Foobar\nFirst Name: David\nLast Name: Johnson\nFoobaz\nBarBaz";

NSScanner *textScanner = [NSScanner scannerWithString:text];

if ([textScanner scanUpToString:@"\nFirst Name: " intoString:NULL]) {
    [textScanner scanString:@"First Name: " intoString:NULL];
    [textScanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&firstName];
}

// if last name always comes after first name there's no need to do this:
[textScanner setScanLocation:0];

if ([textScanner scanUpToString:@"\nLast Name: " intoString:NULL]) {
    [textScanner scanString:@"Last Name: " intoString:NULL];
    [textScanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&lastName];
}    

NSLog(@"%@ %@", firstName, lastName);

I would use a NSScanner. Like this:

NSString *lastName;
NSString *firstName;
NSString *text = @"Foobar\nFirst Name: David\nLast Name: Johnson\nFoobaz\nBarBaz";

NSScanner *textScanner = [NSScanner scannerWithString:text];

if ([textScanner scanUpToString:@"\nFirst Name: " intoString:NULL]) {
    [textScanner scanString:@"First Name: " intoString:NULL];
    [textScanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&firstName];
}

// if last name always comes after first name there's no need to do this:
[textScanner setScanLocation:0];

if ([textScanner scanUpToString:@"\nLast Name: " intoString:NULL]) {
    [textScanner scanString:@"Last Name: " intoString:NULL];
    [textScanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&lastName];
}    

NSLog(@"%@ %@", firstName, lastName);
酒中人 2025-01-12 21:08:06

首先使用 componentsSeparatedByString 根据行拆分字符串

NSArray* lines = [paragraph compontentsSeparatedByString:@"\n"];

然后您可以过滤出以名字开头的行

NSPredicate* firstNamePredicate = [NSPredicate predicateWithFormat:@"SELF beginswith 'First Name:'"];
NSArray* filteredArray = [lines filteredArrayUsingPredicate:firstNamePredicate];

然后您可以根据空格拆分并查找“Name:”标记后面的单词。

First split the string based on lines using componentsSeparatedByString

NSArray* lines = [paragraph compontentsSeparatedByString:@"\n"];

Then you can filter out which lines start with First Name

NSPredicate* firstNamePredicate = [NSPredicate predicateWithFormat:@"SELF beginswith 'First Name:'"];
NSArray* filteredArray = [lines filteredArrayUsingPredicate:firstNamePredicate];

Then you can split based on spaces and look for the word following the "Name:" token.

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