开发者

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

开发者 https://www.devze.com 2023-02-22 23:43 出处:网络
I have some string开发者_如何学Go name and I want to compare whether that string contains substring like \"_thumb.png\". [string rangeOfString:string1].location!=NSNotFound

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/

0

精彩评论

暂无评论...
验证码 换一张
取 消