假设我有一个字符串“Test Super”
我将如何检测“超级”并让函数返回“是”?
像这样的东西:
return (([@"Test Super" rangeOfString:@"Super"]).location != NSNotFound);
使用rangeOfString:
并检查结果的位置是否等于NSNotFound
.
[@"Test Super" rangeOfString:@"Super"].location != NSNotFound //YES
[@"Test Super" rangeOfString:@"Supper"].location != NSNotFound //NO
一个快速的谷歌发现这个: http: //objcolumnist.com/2009/04/12/does-a-nsstring-contain-a-substring/
NSRange 文本范围;textRange =[字符串 rangeOfString:substring];
if(textRange.location != NSNotFound)
{
//Does contain the substring
}