如何检测字符串是否包含某个单词?例如,我有一个字符串,其下面是:
@"Here is my string."
我想知道我是否可以检测字符串中的单词,例如“是”。
答案 0 :(得分:191)
我将如何做到这一点:
NSString *someString = @"Here is my string";
NSRange isRange = [someString rangeOfString:@"is " options:NSCaseInsensitiveSearch];
if(isRange.location == 0) {
//found it...
} else {
NSRange isSpacedRange = [someString rangeOfString:@" is " options:NSCaseInsensitiveSearch];
if(isSpacedRange.location != NSNotFound) {
//found it...
}
}
您可以轻松地将其作为类别添加到NSString
:
@interface NSString (JRStringAdditions)
- (BOOL)containsString:(NSString *)string;
- (BOOL)containsString:(NSString *)string
options:(NSStringCompareOptions)options;
@end
@implementation NSString (JRStringAdditions)
- (BOOL)containsString:(NSString *)string
options:(NSStringCompareOptions)options {
NSRange rng = [self rangeOfString:string options:options];
return rng.location != NSNotFound;
}
- (BOOL)containsString:(NSString *)string {
return [self containsString:string options:0];
}
@end
答案 1 :(得分:89)
使用以下代码扫描句子中的单词。
NSString *sentence = @"The quick brown fox";
NSString *word = @"quack";
if ([sentence rangeOfString:word].location != NSNotFound) {
NSLog(@"Yes it does contain that word");
}
答案 2 :(得分:22)
在iOS8中,您现在可以使用:
BOOL containsString = [@"Here is my string." containsString:@"is"];
关于如何改造"这是一篇有趣的帖子。它到iOS7这里: http://petersteinberger.com/blog/2014/retrofitting-containsstring-on-ios-7/
答案 3 :(得分:3)
我建议使用NSLinguisticTagger。我们可以使用它来搜索Here is my string. His isn't a mississippi isthmus. It is?
NSLinguisticTagger *linguisticTagger = [[NSLinguisticTagger alloc] initWithTagSchemes:@[
NSLinguisticTagSchemeTokenType,
]
options:
NSLinguisticTaggerOmitPunctuation |
NSLinguisticTaggerOmitWhitespace |
NSLinguisticTaggerOmitOther ];
[linguisticTagger setString:@"Here is my string. His isn't a mississippi isthmus. It is?"];
[linguisticTagger enumerateTagsInRange:NSMakeRange(0,
[[linguisticTagger string] length])
scheme:NSLinguisticTagSchemeTokenType
options:
NSLinguisticTaggerOmitPunctuation |
NSLinguisticTaggerOmitWhitespace |
NSLinguisticTaggerOmitOther |
NSLinguisticTaggerJoinNames
usingBlock:^(NSString *tag, NSRange tokenRange, NSRange sentenceRange, BOOL *stop) {
NSLog(@"tag: %@, tokenRange: %@, sentenceRange: %@, token: %@",
tag,
NSStringFromRange(tokenRange),
NSStringFromRange(sentenceRange),
[[linguisticTagger string] substringWithRange:tokenRange]);
}];
输出:
tag: Word, tokenRange: {0, 4}, sentenceRange: {0, 19}, token: Here
tag: Word, tokenRange: {5, 2}, sentenceRange: {0, 19}, token: is
tag: Word, tokenRange: {8, 2}, sentenceRange: {0, 19}, token: my
tag: Word, tokenRange: {11, 6}, sentenceRange: {0, 19}, token: string
tag: Word, tokenRange: {19, 3}, sentenceRange: {19, 33}, token: His
tag: Word, tokenRange: {23, 2}, sentenceRange: {19, 33}, token: is
tag: Word, tokenRange: {25, 3}, sentenceRange: {19, 33}, token: n't
tag: Word, tokenRange: {29, 1}, sentenceRange: {19, 33}, token: a
tag: Word, tokenRange: {31, 11}, sentenceRange: {19, 33}, token: mississippi
tag: Word, tokenRange: {43, 7}, sentenceRange: {19, 33}, token: isthmus
tag: Word, tokenRange: {52, 2}, sentenceRange: {52, 6}, token: It
tag: Word, tokenRange: {55, 2}, sentenceRange: {52, 6}, token: is
它会忽略His
mississippi
和isthmus
,甚至会识别is
内的isn't
。
答案 4 :(得分:3)
我希望这可以帮助你,..在.m文件中添加此行或创建一个单独的类并集成此代码。
@implementation NSString (Contains)
- (BOOL) containsString: (NSString*) substring
{
NSRange range = [self rangeOfString : substring];
BOOL found = ( range.location != NSNotFound );
return found;
}
@end
答案 5 :(得分:2)
使用iOS 8和Swift,我们可以使用localizedCaseInsensitiveContainsString方法
let string: NSString = "Café"
let substring: NSString = "É"
string.localizedCaseInsensitiveContainsString(substring) // true
答案 6 :(得分:0)
完整的解决方案首先扫描字符串(不添加空格),然后检查前一个字符是空白还是行首。同样,检查紧随其后的字符是空白还是行尾。如果两个测试都通过,那么你就匹配了。根据您的需要,您还可以检查,
,.
,()
等。
当然,另一种方法是将字符串解析为单词并单独检查每个单词。