NSString:在另一个字符串中查找字符串的部分
我知道如何在另一个字符串中查找一个字符串,这很容易。但在本例中,我想在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用正则表达式,我更喜欢使用它。请参阅 RegexKitLite。
使用 RegexKitLite,您可以使用正则表达式类似(未经测试):
使用 RegexKitLite,您也可以使用 [NSString stringByMatching:(NSString*)]。
使用正则表达式确实可以做很多事情。通过使用 RegexKitLite 可以使用大量不同的功能。上面的正则表达式应该找到姓氏为 Smith 的人。
正则表达式解释:
(?i)
使大小写不敏感Smith
匹配 Smith 的姓氏。显然,您可以将其更改为任何内容,
匹配逗号\\s*
匹配任意数量的空格(贪婪)\\w
匹配单词另外,您可以使用 [NSString rangeOfString:options:] 函数如下:
另请参阅类似的函数,例如 [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):
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 insensitiveSmith
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 wordAlso, you could use [NSString rangeOfString:options:] function like:
Also see similar functions like [rangeOfString:options:range:locale:] so that you can do case insensitive searches and even specify a locale.