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

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


[string rangeOfString:string1].location!=NSNotFound

rangeOfString:文档。


You can try this:

NSString *originalString;
NSString *compareString;

Let your string be stored in originalString and the substring that you want to compare is stored in compareString .

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

ssteinberg's answer looks pretty good, but there is also this:

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

if(textRange.location != NSNotFound)
{

//Does contain the substring
}

which I found here.

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

上一篇: iPhone:在另一个情况下,发现一个字符串不起作用

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