NSString:在另一个字符串中查找字符串的部分

发布于 2024-12-05 20:10:56 字数 943 浏览 0 评论 0原文

我知道如何在另一个字符串中查找一个字符串,这很容易。但在本例中,我想在 allProfessors 字符串中找到 John Smith。所以我想我可以分割字符串并搜索两个部分,这按照我想要的方式工作:

NSString *fullName = @"John Smith";
NSArray *parts = [fullName componentsSeparatedByString:@" "];
NSString *allProfessors = @"Smith, John; Clinton, Bill; Johnson, John";
NSRange range = [[allProfessors lowercaseString] rangeOfString:[[parts objectAtIndex:0] lowercaseString]];
NSRange range2 = [[allProfessors lowercaseString] rangeOfString:[[parts objectAtIndex:1] lowercaseString]];
if(range.location != NSNotFound && range2.location != NSNotFound) {
    NSLog(@"Found");
} else {
    NSLog(@"Not Found");
}

我想知道的是,这是执行此操作的最佳方法还是有更优选的方法来执行我的操作

除此之外,如果我的全名比我的 allProfessors 姓名长怎么办,例如:

NSString *fullName = @"Gregory Smith";
NSString *allProfessors = @"Smith, Greg; Clinton, Bill; Johnson, John";

我仍然希望 Greg Smith 和 Gregory Smith 匹配。

I know how to find a string in another string, that is easy. But in this case I want to find John Smith within the allProfessors string. So I figured I could just split the string and search for both parts, which works how I want:

NSString *fullName = @"John Smith";
NSArray *parts = [fullName componentsSeparatedByString:@" "];
NSString *allProfessors = @"Smith, John; Clinton, Bill; Johnson, John";
NSRange range = [[allProfessors lowercaseString] rangeOfString:[[parts objectAtIndex:0] lowercaseString]];
NSRange range2 = [[allProfessors lowercaseString] rangeOfString:[[parts objectAtIndex:1] lowercaseString]];
if(range.location != NSNotFound && range2.location != NSNotFound) {
    NSLog(@"Found");
} else {
    NSLog(@"Not Found");
}

What I want to know is, is this the BEST way to do this or is there a more preferred method to do what I want?

In addition to this, what if my fullName is longer than my allProfessors name, such as:

NSString *fullName = @"Gregory Smith";
NSString *allProfessors = @"Smith, Greg; Clinton, Bill; Johnson, John";

I still want there to be a match for Greg Smith and Gregory Smith.

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

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

发布评论

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

评论(1

放赐 2024-12-12 20:10:56

您可以使用正则表达式,我更喜欢使用它。请参阅 RegexKitLite

使用 RegexKitLite,您可以使用正则表达式类似(未经测试):

NSString *regEx = @"(?i)Smith,\\s*\\w";
NSArray *matchingStrings = [allProfessors componentsMatchedByRegex:regEx];

if ([matchingStrings count] == 0)  // not found!
{
   [...]
}
else
{
   [...]
}

使用 RegexKitLite,您也可以使用 [NSString stringByMatching:(NSString*)]。

使用正则表达式确实可以做很多事情。通过使用 RegexKitLite 可以使用大量不同的功能。上面的正则表达式应该找到姓氏为 Smith 的人。

正则表达式解释:

  • (?i) 使大小写不敏感
  • Smith 匹配 Smith 的姓氏。显然,您可以将其更改为任何内容
  • , 匹配逗号
  • \\s* 匹配任意数量的空格(贪婪)
  • \\w 匹配单词

另外,您可以使用 [NSString rangeOfString:options:] 函数如下:

if ([myString rangeOfString:@"John" options:NSCaseInsensitiveSearch].location != NSNotFound &&
    [myString rangeOfString:@"Smith" options:NSCaseInsensitiveSearch].location != NSNotFound)
{
   NSLog(@"Found");
}
else
{
   NSLog(@"Not Found");
}

另请参阅类似的函数,例如 [rangeOfString:options:range:locale:] 以便您可以执行不区分大小写的搜索,甚至指定区域设置。

You could use regular expressions, which I prefer to use. See RegexKitLite.

With RegexKitLite, you could use a regular expression like (untested):

NSString *regEx = @"(?i)Smith,\\s*\\w";
NSArray *matchingStrings = [allProfessors componentsMatchedByRegex:regEx];

if ([matchingStrings count] == 0)  // not found!
{
   [...]
}
else
{
   [...]
}

Using RegexKitLite you could alternatively have used [NSString stringByMatching:(NSString*)].

You can really do a lot with regular expression. There are a ton of different functions available through Using RegexKitLite. The regular expression above should find people with the last name of Smith.

Regular Expression explained:

  • (?i) make this case insensitive
  • Smith matches last name of Smith. Obviously you could change this to anything
  • , match a comma
  • \\s* match any number of spaces (greedy)
  • \\w match a word

Also, you could use [NSString rangeOfString:options:] function like:

if ([myString rangeOfString:@"John" options:NSCaseInsensitiveSearch].location != NSNotFound &&
    [myString rangeOfString:@"Smith" options:NSCaseInsensitiveSearch].location != NSNotFound)
{
   NSLog(@"Found");
}
else
{
   NSLog(@"Not Found");
}

Also see similar functions like [rangeOfString:options:range:locale:] so that you can do case insensitive searches and even specify a locale.

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