我正在尝试检查要用作URL的字符串是否以http开头。我现在尝试检查的方式似乎不起作用。这是我的代码:
NSMutableString *temp = [[NSMutableString alloc] initWithString:@"http://"];
if ([businessWebsite rangeOfString:@"http"].location == NSNotFound){
NSString *temp2 = [[NSString alloc] init];
temp2 = businessWebsite;
[temp appendString:temp2];
businessWebsite = temp2;
NSLog(@"Updated BusinessWebsite is: %@", businessWebsite);
}
[web setBusinessWebsiteUrl:businessWebsite];
有任何想法吗?
最佳答案
试试这个:if ([myString hasPrefix:@"http"])
。
顺便说一句,您的测试应该是!= NSNotFound
而不是== NSNotFound
。但是请说您的网址是ftp://my_http_host.com/thing
,它将匹配但不应该匹配。