【发布时间】:2011-07-30 17:15:02
【问题描述】:
我有一些字符串名称,我想比较该字符串是否包含像“_thumb.png”这样的子字符串。
【问题讨论】:
-
请在此处查看更完整的答案:stackoverflow.com/questions/2753956/…
标签: iphone objective-c cocoa-touch ios4
我有一些字符串名称,我想比较该字符串是否包含像“_thumb.png”这样的子字符串。
【问题讨论】:
标签: iphone objective-c cocoa-touch ios4
[string rangeOfString:string1].location!=NSNotFound
rangeOfString: 文档。
【讨论】:
你可以试试这个:
NSString *originalString;
NSString *compareString;
让您的string 存储在originalString 中,而您要比较的substring 存储在compareString 中。
if ([originalString rangeOfString:compareString].location==NSNotFound)
{
NSLog(@"Substring Not Found");
}
else
{
NSLog(@"Substring Found Successfully");
}
【讨论】:
ssteinberg 的回答看起来还不错,不过也有这样的:
NSRange textRange;
textRange =[string rangeOfString:substring];
if(textRange.location != NSNotFound)
{
//Does contain the substring
}
我找到了here。
【讨论】:
NSString *string = @"hello bla bla";
if ([string rangeOfString:@"bla"].location == NSNotFound)
{
NSLog(@"string does not contain bla");
}
else
{
NSLog(@"string contains bla!");
}
【讨论】:
在ios 8 或操作系统X 10.10 我们可以使用。
if(![textLine containsString:@"abc"])
{
[usableLines addObject:textLine];
}
【讨论】: