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

时间:2021-07-01 07:16:01

I have some string name and I want to compare whether that string contains substring like "_thumb.png".

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

6 个解决方案

#1


62  

[string rangeOfString:string1].location!=NSNotFound

rangeOfString: documentation.

rangeOfString:documentation。

#2


16  

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.

让您的字符串存储在originalString中,您要比较的子字符串存储在compareString中。

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

#3


3  

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.

我在这里找到了。

#4


2  

NSString *string = @"hello bla bla";
if ([string rangeOfString:@"bla"].location == NSNotFound)
{
    NSLog(@"string does not contain bla");
}  
else 
{
    NSLog(@"string contains bla!");
}

#5


1  

In ios 8 or OS X 10.10 we can use.

在ios 8或OS X 10.10中我们可以使用。

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

#6


0  

u can go through this

你可以通过这个

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

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

#1


62  

[string rangeOfString:string1].location!=NSNotFound

rangeOfString: documentation.

rangeOfString:documentation。

#2


16  

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.

让您的字符串存储在originalString中,您要比较的子字符串存储在compareString中。

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

#3


3  

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.

我在这里找到了。

#4


2  

NSString *string = @"hello bla bla";
if ([string rangeOfString:@"bla"].location == NSNotFound)
{
    NSLog(@"string does not contain bla");
}  
else 
{
    NSLog(@"string contains bla!");
}

#5


1  

In ios 8 or OS X 10.10 we can use.

在ios 8或OS X 10.10中我们可以使用。

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

#6


0  

u can go through this

你可以通过这个

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

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