验证NSString中是否存在单词 [英] Verify the existence of a word in an NSString
本文介绍了验证NSString中是否存在单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我搜索了一下,但找不到这个问题的答案(可能很简单)。
I searched a bit, but couldn't find an answer to this (probably very simple) question.
我有一个NSString,我想检查它是否包含一个单词。像这样:
I have an NSString, and I'd like to check if it contains a word. Something like this:
NSString *sentence = @"The quick brown fox";
NSString *word = @"quack";
if ([sentence containsWord:word]) {
NSLog(@"Yes it does contain that word");
}
感谢。
推荐答案
以下应该可以工作:
NSString *sentence = @"The quick brown fox";
NSString *word = @"quack";
if ([sentence rangeOfString:word].location != NSNotFound) {
NSLog(@"Yes it does contain that word");
}
它使用 rangeOfString:
返回 NSRange
结构,指示单词的位置,如果找不到它 NSRange.location
将等于 NSNotFound
。
It uses rangeOfString:
to return an NSRange
structure, indicating the location of the word, if it can't find it NSRange.location
will be equal to NSNotFound
.
这篇关于验证NSString中是否存在单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文