iPhone : How to check whether a substring exists in a string?
I have some string开发者_如何学Go name and I want to compare whether that string contains substring like "_thumb.png".
[string rangeOfString:string1].location!=NSNotFound
rangeOfString: documentation.
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.
NSString *string = @"hello bla bla";
if ([string rangeOfString:@"bla"].location == NSNotFound)
{
NSLog(@"string does not contain bla");
}
else
{
NSLog(@"string contains bla!");
}
In ios 8
or OS X 10.10
we can use.
if(![textLine containsString:@"abc"])
{
[usableLines addObject:textLine];
}
u can go through this
http://objcolumnist.com/2009/04/12/does-a-nsstring-contain-a-substring/
精彩评论