Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting a URL from within a string

My app receives numerous text strings which may or may not contain a URL anywhere within the string. What would be the best method to extract a URL from within a string? Thank you.

like image 408
RunLoop Avatar asked Aug 17 '26 06:08

RunLoop


1 Answers

If you are working on a Mac application, Mac OS X 10.6 offers a new API to let you detect URLs with the spell checker. You may do it this way.

NSString *s = @"http://example.com"
NSInteger wordCount = 0;
NSOrthography *orthography = nil;
NSArray *checkResults = [[NSSpellChecker sharedSpellChecker] checkString:s range:NSMakeRange(0, [s length]) types:NSTextCheckingTypeLink options:nil inSpellDocumentWithTag:0 orthography:&orthography wordCount:&wordCount];
for (NSTextCheckingResult *result in checkResults) {
    NSRange range = result.range;
    NSURL *URL = result.URL;
}
like image 141
zonble Avatar answered Aug 19 '26 21:08

zonble



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!