Iphone 如何检查字符串是否包含URL
我有一条文本消息,我想检查它是否包含文本“http”或URL 我怎么检查Iphone 如何检查字符串是否包含URL,iphone,nsstring,Iphone,Nsstring,我有一条文本消息,我想检查它是否包含文本“http”或URL 我怎么检查 NSString *string = @"xxx http://someaddress.com"; NSString *substring = @"http:"; 区分大小写的示例: NSRange textRange = [string rangeOfString:substring]; if(textRange.location != NSNotFound){ //Does contain the subs
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
}
不区分大小写示例:
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
}
@塞浦路斯提供了一个很好的选择
您还可以考虑使用一个更灵活的Url,假设这是您所需要的,例如,如果您想匹配http://和https://的话。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"]