iPhone:如何检查字符串中是否存在子字符串?

我有一些字符串名称,我想比较该字符串是否包含像“_thumb.png”这样的子字符串。


[string rangeOfString:string1].location!=NSNotFound

rangeOfString:文档。


你可以试试这个:

NSString *originalString;
NSString *compareString;

让您的string存储在originalString并且您要比较的substring存储在compareString

if ([originalString rangeOfString:compareString].location==NSNotFound)
{       
    NSLog(@"Substring Not Found");  
}
else
{
    NSLog(@"Substring Found Successfully");
}

ssteinberg的答案看起来不错,但也有这样的:

NSRange textRange;
textRange =[string rangeOfString:substring];

if(textRange.location != NSNotFound)
{

//Does contain the substring
}

我在这里找到了。

链接地址: http://www.djcxy.com/p/73991.html

上一篇: iPhone : How to check whether a substring exists in a string?

下一篇: Is there a way to use if 'contains' instead of isEqual?