Rob*_*Rob 150 iphone objective-c nsstring nsmutablestring ios
我试图检查我将用作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];
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
Cyr*_*lle 327
试试这个:if ([myString hasPrefix:@"http"])
.
顺便说一句,你的测试应该是!= NSNotFound
代替== NSNotFound
.但是说你的网址是ftp://my_http_host.com/thing
,它会匹配,但不应该.
Jon*_*asG 22
我喜欢用这个方法:
if ([[temp substringToIndex:4] isEqualToString:@"http"]) {
//starts with http
}
Run Code Online (Sandbox Code Playgroud)
甚至更容易:
if ([temp hasPrefix:@"http"]) {
//do your stuff
}
Run Code Online (Sandbox Code Playgroud)
如果您正在检查"http:",您可能需要不区分大小写的搜索:
NSRange prefixRange =
[temp rangeOfString:@"http"
options:(NSAnchoredSearch | NSCaseInsensitiveSearch)];
if (prefixRange.location == NSNotFound)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
65842 次 |
最近记录: |