如何找出是一个长字符串,一个字obj c,iphone

Bde*_*317 0 string iphone compare objective-c

我有一个长字符串,其中包含文本框中所有"输入的单词"的运行列表.我希望能够针对一个单词字符串检查长字符串,以查看长字符串是否包含短字符串中的单词.

有任何想法吗?

我已经尝试了这个和其他一些东西,其中newString是长字符串,currentTextrightnow是短字符串.

textRange =[newString rangeOfString:currentTextrightnow];
NSLog(@"currenttextright now is %@", currentTextrightnow);
if(textRange.location != NSNotFound)

{

    NSLog(@"Does contatin the substring");

}
Run Code Online (Sandbox Code Playgroud)

Rud*_*ger 6

我遇到了和你一样的问题.你实际上是正确的(如果textRange是NSRange类型)但问题是NSNotFound不能按预期工作(可能是一个错误).相反,你可以做到

if (range.length > 0) {
    //it found it
} else {
    // it didn't find it
}
Run Code Online (Sandbox Code Playgroud)

希望有所帮助.

  • 为什么发布它有效并且没有标记正确?真的吗? (4认同)