如何检查字符串是否包含 URL

发布于 2024-11-27 23:47:13 字数 55 浏览 0 评论 0原文

我有短信,我想检查它是否包含文本“http”或其中存在 URL。

我将如何检查?

i have text message and I want to check whether it is containing text "http" or URL exists in that.

How will I check it?

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

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

发布评论

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

评论(3

冧九 2024-12-04 23:47:13
NSString *string = @"xxx http://someaddress.com";
NSString *substring = @"http:";

区分大小写的示例:

NSRange textRange = [string rangeOfString:substring];

if(textRange.location != NSNotFound){
    //Does contain the substring
}else{
    //Does not contain the substring
}

不区分大小写的示例:

NSRange textRange = [[string lowercaseString] rangeOfString:[substring lowercaseString]];

if(textRange.location != NSNotFound){
    //Does contain the substring
}else{
    //Does not contain the substring
}
NSString *string = @"xxx http://someaddress.com";
NSString *substring = @"http:";

Case sensitive example:

NSRange textRange = [string rangeOfString:substring];

if(textRange.location != NSNotFound){
    //Does contain the substring
}else{
    //Does not contain the substring
}

Case insensitive example:

NSRange textRange = [[string lowercaseString] rangeOfString:[substring lowercaseString]];

if(textRange.location != NSNotFound){
    //Does contain the substring
}else{
    //Does not contain the substring
}
花开雨落又逢春i 2024-12-04 23:47:13

@Cyprian 提供了一个不错的选择。

您还可以考虑使用 NSRegularExpression 假设这正是您所需要的,这将为您提供更大的灵活性,例如,如果您想要匹配 http:// 和 https:// 。

@Cyprian offers a good option.

You could also consider using a NSRegularExpression which would give you far more flexibility assuming that's what you need, e.g. if you wanted to match http:// and https://.

装迷糊 2024-12-04 23:47:13

Url 通常包含 http 或 https

您可以使用自定义方法 containsString 来检查这些字符串。

- (BOOL)containsString:(NSString *)string {
    return [self containsString:string caseSensitive:NO];
}
- (BOOL)containsString:(NSString*)string caseSensitive:(BOOL)caseSensitive {
    BOOL contains = NO;
    if (![NSString isNilOrEmpty:self] && ![NSString isNilOrEmpty:string]) {
        NSRange range;
        if (!caseSensitive) {
            range =  [self rangeOfString:string options:NSCaseInsensitiveSearch];
        } else {
            range =  [self rangeOfString:string];
        }
        contains = (range.location != NSNotFound);
    }

    return contains;
}

例子 :

[yourString containsString:@"http"]

[yourString containsString:@"https"] 

Url usually has http or https in it

You can use your custom method containsString to check for those strings.

- (BOOL)containsString:(NSString *)string {
    return [self containsString:string caseSensitive:NO];
}
- (BOOL)containsString:(NSString*)string caseSensitive:(BOOL)caseSensitive {
    BOOL contains = NO;
    if (![NSString isNilOrEmpty:self] && ![NSString isNilOrEmpty:string]) {
        NSRange range;
        if (!caseSensitive) {
            range =  [self rangeOfString:string options:NSCaseInsensitiveSearch];
        } else {
            range =  [self rangeOfString:string];
        }
        contains = (range.location != NSNotFound);
    }

    return contains;
}

Example :

[yourString containsString:@"http"]

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