how to find out is a long string contatins a word obj c, iphone

我们两清 提交于 2019-12-14 04:23:03

问题


I have a long string that has a running list of all the "words entered" in the text box. I want to be able to check the long string against a one word string to see if the long string contains the word in the short string.

Any ideas?

I've tried this and a few other things, where newString is the long string and currentTextrightnow is the short string.

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

{

    NSLog(@"Does contatin the substring");

}

回答1:


I had the same problem as you did. You are actually doing it right (if textRange is of type NSRange) but the problem is NSNotFound doesn't work as expected (might be a bug). Instead you can do

if (range.length > 0) {
    //it found it
} else {
    // it didn't find it
}

Hope that helps.



来源:https://stackoverflow.com/questions/3877835/how-to-find-out-is-a-long-string-contatins-a-word-obj-c-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!