问题
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