Yuv*_*j.M 1 iphone url http nsstring
我在NSString中有一个带http://的文本.我想从NSString获取该http链接.如何从字符串中获取链接/ url?例如:'Stack over flow对于初学者来说是非常有用的链接 https://stackoverflow.com/ '.我想从文本中获取" https://stackoverflow.com/ ".我怎样才能做到这一点?提前致谢.
我不确定你的链接是什么意思,但如果你想将你的NSString转换为NSURL,你可以执行以下操作:
NSString *urlString = @"http://somepage.com";
NSURL *url = [NSURL URLWithString:urlString];
Run Code Online (Sandbox Code Playgroud)
编辑
这是如何获取给定NSString中的所有URL:
NSString *str = @"This is a grate website http://xxx.xxx/xxx you must check it out";
NSArray *arrString = [str componentsSeparatedByString:@" "];
for(int i=0; i<arrString.count;i++){
if([[arrString objectAtIndex:i] rangeOfString:@"http://"].location != NSNotFound)
NSLog(@"%@", [arrString objectAtIndex:i]);
}
Run Code Online (Sandbox Code Playgroud)