![](/img/trans.png)
[英]Determine whether array contains one or more elements of another array in swift
[英]Checking whether elements of an Array contains a specified String
我正在开发一个 iOS 应用程序。 我有一个数组,
NSArray *qwer=[NSArray arrayWithObjects:@"Apple Juice",@"Apple cake",@"Apple chips",@"Apple wassail"nil];
for(int k=0; k<qwer.count; k++) {
//
}
我正在尝试检查我的数组是否包含字符串“ wassail ”。 谁能帮我找到这个?
NSString
有一个rangeOfString
函数,可用于查找部分字符串匹配。 此函数返回NSRange
。 您可以使用location
属性。
...
NSArray *qwer = [NSArray arrayWithObjects:@"Apple Juice",@"Apple cake",@"Apple chips",@"Apple wassail"nil];
for (NSString *name in qwer){
if ([name rangeOfString:keyword].location == NSNotFound) {
NSLog(@"contains");
}
}
...
参考 :
谢谢斯里拉姆
试试这个
NSArray *qwer=[NSArray arrayWithObjects:@"Apple Juice",@"Apple cake",@"Apple chips",@"Apple wassail",nil];
for(int k=0; k<qwer.count; k++) {
NSString *currentString =[qwer objectAtIndex:k];
if ([currentString rangeOfString:@"wassail"].location == NSNotFound) {
NSLog(@"string does not contain wassail");
} else {
NSLog(@"string contains wassail!");
break;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.