如何在 iPhone 中查找和获取 NSString 中的 URL?

发布于 2024-12-23 11:05:41 字数 271 浏览 2 评论 0原文

我在 NSString 中有一个带有 http:// 的文本。我想从 NSString 获取 http 链接。如何从字符串中获取链接/url?例如:“Stack over flow 对于初学者来说是非常有用的链接https://stackoverflow.com/”。我想从文本中获取“https://stackoverflow.com/”。我该怎么做?提前致谢。

I have a text with http:// in NSString. I want to get that http link from the NSString. How can i get the link/url from the string? Eg: 'Stack over flow is very useful link for the beginners https://stackoverflow.com/'. I want to get the 'https://stackoverflow.com/' from the text. How can i do this? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半世蒼涼 2024-12-30 11:05:41

我不确定链接的确切含义,但如果您想将 NSString 转换为 NSURL,则可以执行以下操作:

NSString *urlString = @"http://somepage.com";
NSURL *url = [NSURL URLWithString:urlString];

编辑

这是获取给定 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]);
}

I am not sure what you exactly mean by link but if you want to convert your NSString to NSURL than you can do the following:

NSString *urlString = @"http://somepage.com";
NSURL *url = [NSURL URLWithString:urlString];

EDIT

This is how to get all URLs in a given NSString:

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]);
}
紅太極 2024-12-30 11:05:41

您可以只搜索以 @"http://" 开头的子字符串,而不是将字符串拆分为数组并以这种方式搞乱:

NSString *str = @"Stack over flow is very useful link for the beginners http://stackoverflow.com/";
// get the range of the substring starting with @"http://"
NSRange rng = [str rangeOfString:@"http://" options:NSCaseInsensitiveSearch];

// Set up the NSURL variable to hold the created URL
NSURL *newURL = nil;

// Make sure that we actually have found the substring
if (rng.location == NSNotFound) {
    NSLog(@"URL not found");
    // newURL is initialised to nil already so nothing more to do.
} else {
    // Get the substring from the start of the found substring to the end.
    NSString *urlString = [str substringFromIndex:rng.location];

    // Turn the string into an URL and put it into the declared variable
    newURL = [NSURL URLWithString:urlString];
}

Rather than splitting the string into an array and messing about that way, you can just search for the substring beginning with @"http://":

NSString *str = @"Stack over flow is very useful link for the beginners http://stackoverflow.com/";
// get the range of the substring starting with @"http://"
NSRange rng = [str rangeOfString:@"http://" options:NSCaseInsensitiveSearch];

// Set up the NSURL variable to hold the created URL
NSURL *newURL = nil;

// Make sure that we actually have found the substring
if (rng.location == NSNotFound) {
    NSLog(@"URL not found");
    // newURL is initialised to nil already so nothing more to do.
} else {
    // Get the substring from the start of the found substring to the end.
    NSString *urlString = [str substringFromIndex:rng.location];

    // Turn the string into an URL and put it into the declared variable
    newURL = [NSURL URLWithString:urlString];
}
身边 2024-12-30 11:05:41

试试这个:

nsstring *str = @"Stack over flow is very useful link for the beginners http://stackoverflow.com/";

nsstring *http = @"http";
nsarray *arrURL = [str componentsSeparatedByString:@"http"];

这将在 nsarray 中给出两个对象。第一个对象将具有:Stack over flow对于初学者来说非常有用的链接,第二个对象将是:://stackoverflow.com/(我猜)

那么你可以这样做喜欢:

  NSString *u = [arrURL lastObject];

然后喜欢:

nsstring *http = [http stringByAppendingFormat:@"%@",u];

相当长,但我认为这对你有用。希望对您有帮助。

try this :

nsstring *str = @"Stack over flow is very useful link for the beginners http://stackoverflow.com/";

nsstring *http = @"http";
nsarray *arrURL = [str componentsSeparatedByString:@"http"];

this will give two objects in the nsarray. 1st object will be having:Stack over flow is very useful link for the beginners and 2nd will be : ://stackoverflow.com/ (i guess)

then you can do like:

  NSString *u = [arrURL lastObject];

then do like:

nsstring *http = [http stringByAppendingFormat:@"%@",u];

Quite a lengthy,but i think that would work for you. Hope that helps you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文