简体   繁体   English

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

[英]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 . 让您的string存储在originalString ,而要比较的substring存储在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: ssteinberg的答案看起来不错,但还有以下几点:

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. ios 8或OS X 10.10我们可以使用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM