搜索具有 nsmutablearray 内容的字符串

发布于 2024-10-09 03:50:58 字数 242 浏览 0 评论 0原文

这可能是一个快速而简单的问题,但是我如何能够使用 nsmutablearray 的内容(即字符串)搜索字符串。所以我有 NSString *blah = @"djfald.ji"。我有 nsmutablearray 填充了不同的扩展名,我想搜索字符串 blah 以查看是否有任何扩展名匹配。我曾经使用 -[NSRange rangeOfString:] 但这不适用于数组。

谢谢,

凯文

This is probably a quick and easy question, but how would I be able to search a string with the contents of a nsmutablearray which are strings. So I have the NSString *blah = @"djfald.ji". I have the nsmutablearray filled with different extensions and I want to search the string blah to see if any of the extensions have a match. I used to use -[NSRange rangeOfString:] but that doesn't work with arrays.

Thanks,

Kevin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

信愁 2024-10-16 03:50:58

如果您确实正在处理路径扩展,那么最好以相反的方式进行处理。像这样的东西:

NSString *extension = [@"djfald.ji" pathExtension];
BOOL found = [extensions containsObject:extension];

If you really are dealing with path extensions, it's probably better to approach this the other way round. Something like:

NSString *extension = [@"djfald.ji" pathExtension];
BOOL found = [extensions containsObject:extension];
狼亦尘 2024-10-16 03:50:58

只需使用块:

NSUInteger extIndex = [extensionArray indexOfObjectPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {
  return [blah hasSuffix:obj];
}];
NSString *extension = extensionIndex == NSNotFound ? [extensionArray objectAtIndex extIndex] : nil;

或者只需使用枚举循环遍历数组。

Simply use a block:

NSUInteger extIndex = [extensionArray indexOfObjectPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {
  return [blah hasSuffix:obj];
}];
NSString *extension = extensionIndex == NSNotFound ? [extensionArray objectAtIndex extIndex] : nil;

Or simply loop through the array with an enumeration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文