NSRange 可以确定较大字符串中是否存在文本片段吗?

发布于 2024-10-12 07:22:57 字数 344 浏览 5 评论 0原文

我有一个从 http GET 返回的大字符串,我试图确定它是否有特定的文本片段(请原谅我的罪过)

我的问题是这样的:我可以/应该使用 NSRange 来确定这是否文本片段确实存在吗?

  NSRange textRange;
  textRange =[[responseString lowercaseString] rangeOfString:[@"hat" lowercaseString]];

  if(textRange.location != NSNotFound)
  {
    //do something magical with this hat
  }

先感谢您!

I have a large string coming back from an http GET and I'm trying to determine if it has a specific snippet of text or not (please forgive my sins here)

My question is this: Can / Should I use NSRange to determine if this snippet of text does exist?

  NSRange textRange;
  textRange =[[responseString lowercaseString] rangeOfString:[@"hat" lowercaseString]];

  if(textRange.location != NSNotFound)
  {
    //do something magical with this hat
  }

Thank you in advance!

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

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

发布评论

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

评论(2

往日情怀 2024-10-19 07:22:57

您可以检查该位置是否为 NSNotFound

NSRange textRange = [[responseString lowercaseString] rangeOfString:@"hat"];
if (textRange.location == NSNotFound) {
    // "hat" is not in the string
}

如果未找到字符串,则 rangeOfString: 返回 {NSNotFound, 0}

如果您经常使用它,您可以将其捆绑到 NSString 上的一个类别中:

@interface NSString (Helper)
- (BOOL)containsString:(NSString *)s;
@end

@implementation NSString (Helper)

- (BOOL)containsString:(NSString *)s
{
    return [self rangeOfString:s].location != NSNotFound;
}

@end

You can check to see if the location is NSNotFound:

NSRange textRange = [[responseString lowercaseString] rangeOfString:@"hat"];
if (textRange.location == NSNotFound) {
    // "hat" is not in the string
}

If a string is not found, rangeOfString: returns {NSNotFound, 0}.

You could bundle this up into a category on NSString if you use it a lot:

@interface NSString (Helper)
- (BOOL)containsString:(NSString *)s;
@end

@implementation NSString (Helper)

- (BOOL)containsString:(NSString *)s
{
    return [self rangeOfString:s].location != NSNotFound;
}

@end
月朦胧 2024-10-19 07:22:57

iOS 9.2、Xcode 7.2、启用 ARC

感谢“mipadi”的原始贡献。我想详细说明并更新答案。

为什么你仍然使用这种技术?嗯, - (BOOL)containsString:(NSString *)str 仅支持 iOS 8.0 及更高版本。

我最喜欢的用法:

if (yourString)
{
    //Check to make yourString is not nil, otherwise NSInvalidArgumentException is raised.

    if (!([yourString rangeOfString:@"stringToSearchFor"].location == NSNotFound))
    {
        //The string "stringToSearchFor" was found in yourString, i.e. the result is NOT NSNotFound.
    }
    else
    {
        //The string "stringToSearchFor" was not found in yourString.
    }
}
else
{
    nil;
}

希望这对某人有帮助!干杯。

iOS 9.2, Xcode 7.2, ARC enabled

Thanks "mipadi" for the original contribution. I wanted to elaborate and update the answer.

Why would you still use this technique? Well, - (BOOL)containsString:(NSString *)str is only supported iOS 8.0 and later.

My favorite use of this:

if (yourString)
{
    //Check to make yourString is not nil, otherwise NSInvalidArgumentException is raised.

    if (!([yourString rangeOfString:@"stringToSearchFor"].location == NSNotFound))
    {
        //The string "stringToSearchFor" was found in yourString, i.e. the result is NOT NSNotFound.
    }
    else
    {
        //The string "stringToSearchFor" was not found in yourString.
    }
}
else
{
    nil;
}

Hope this helps someone! Cheers.

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