tags:

views:

2169

answers:

2

I have an NSString like that :" abc xyz http://www.abc.com aaa bbb ccc"

How can I get the substring http://www.abc.com from that NSString? Thanks in advance.

+3  A: 

If all of your substrings are separated by spaces, you can try to get an array of substrings using [myString componentsSeparatedByString:@" "]. Then you can check result of [NSUrl URLWithString:yourSubstring]. It will return nil if the substring isn't a correct link.

Morion
Maybe you misunderstood me or maybe I misunderstood you but http://www.abc.com is just an example. What I mean is how to get a substring have format of an url link from the NSString. Waiting for your reply
ThyPhuong
Hmm... NSArray* subStrings = [yourString componentsSeparatedByString:@" "];for(NSString* subString in subStrings){ if([NSUrl URLWithString: subString]){ NSLog(@"This substring is an URL: %@", subString); }}
Morion
If your substrings are separated by @" ", you will see in console all substrings, that are correct URLs.
Morion
+2  A: 

A possible solution might be using regular expressions. Check out RegexKit.

Jonathan Sterling
Regexp is almost always the wrong solution. Especially matching URLs is _very_ hard as the correct regexp is really long. Matching e-mail addresses for example is hard: http://www.regular-expressions.info/email.html
Georg
Point well taken.
Jonathan Sterling