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

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

我有一些字符串名称,我想比较该字符串是否包含类似"_thumb.png"的子字符串。


1
[string rangeOfString:string1].location!=NSNotFound

Rangeofstring:Documentation.


你可以试试这个

ZZU1

让你的string储存在originalStringsubstring中,你想比较的是compareString

1
2
3
4
5
6
7
8
if ([originalString rangeOfString:compareString].location==NSNotFound)
{      
    NSLog(@"Substring Not Found");  
}
else
{
    NSLog(@"Substring Found Successfully");
}


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

1
2
3
4
5
6
7
8
NSRange textRange;
textRange =[string rangeOfString:substring];

if(textRange.location != NSNotFound)
{

//Does contain the substring
}

我在这里找到的


1
2
3
4
5
6
7
8
9
NSString *string = @"hello bla bla";
if ([string rangeOfString:@"bla"].location == NSNotFound)
{
    NSLog(@"string does not contain bla");
}  
else
{
    NSLog(@"string contains bla!");
}

我们可以使用它。

1
2
3
4
if(![textLine containsString:@"abc"])
{
     [usableLines addObject:textLine];
}

你可以穿过这个

http://objcolumnist.com/2009/04/12/does-a-nsstring-contain-a-substring/。