我在Objective-C中編寫代碼。我想從一個字符串中提取一個url。iOS rangeOfString找不到肯定存在的字符串
這裏是我的代碼:
NSMutableString *oneContent = [[latestPosts objectAtIndex:i] objectForKey:@"content"];
NSLog(@"%@", oneContent);//no problem
NSString *string = @"http";
if ([oneContent rangeOfString:string].location == NSNotFound) {
NSLog(@"string does not contain substring");
} else {
NSLog(@"string contains substring!");
}
正如你所看到的,我想提取從oneContent
字符串的URL,我已經檢查了oneContent
肯定包含「HTTP」,但爲什麼結果顯示沒有?
有一些更好的方法來提取網址是什麼?
你可以嘗試這樣的[oneContent componentsSeparatedByString:@ 「stringtoseparate」。這將返回分隔的字符串 –