在 iOS Objective-C 中,可以使用正则表达式来判断一个字符串是文本还是链接。下面是一个示例代码:

- (BOOL)isURL:(NSString *)string {
    NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:'(http|https)://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?' options:NSRegularExpressionCaseInsensitive error:&error];
    
    NSTextCheckingResult *match = [regex firstMatchInString:string options:0 range:NSMakeRange(0, [string length])];
    return match != nil;
}

你可以将要判断的字符串传递给isURL:方法,如果是链接则返回YES,如果是文本则返回NO

iOS Objective-C 判断字符串是否为链接

原文地址: https://www.cveoy.top/t/topic/qjjl 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录