5

I have an NSString which initially looked like <a href="http://link.com"> LinkName</a>. I removed the html tags and now have an NSString that looks like

http://Link.com   SiteName

how can I separate the two into different NSStrings so I would have

http://Link.com

and

SiteName

I specifically want to show the SiteName in a label and just use the http://Link.com to open in a UIWebView but I can't when it is all one string. Any suggestions or help is greatly appreciated.

PengOne
  • 48,188
  • 17
  • 130
  • 149
FreeAppl3
  • 858
  • 1
  • 15
  • 32
  • possible duplicate of [NSString tokenize in Objective-C](http://stackoverflow.com/questions/259956/nsstring-tokenize-in-objective-c) – David Gelhar Oct 12 '11 at 21:05

2 Answers2

8
NSString *s = @"http://Link.com   SiteName";
NSArray *a = [s componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(@"http: '%@'", [a objectAtIndex:0]);
NSLog(@"site: '%@'", [a lastObject]);

NSLog output:

http: 'http://Link.com'
site: 'SiteName'

Bonus, handle a site name with an embedded space with a RE:

NSString *s = @"<a href=\"http://link.com\"> Link Name</a>";
NSString *pattern = @"(http://[^\"]+)\">\\s+([^<]+)<";

NSRegularExpression *regex = [NSRegularExpression
                              regularExpressionWithPattern:pattern
                              options:NSRegularExpressionCaseInsensitive
                              error:nil];

NSTextCheckingResult *textCheckingResult = [regex firstMatchInString:s options:0 range:NSMakeRange(0, s.length)];
NSString *http = [s substringWithRange:[textCheckingResult rangeAtIndex:1]];
NSString *site = [s substringWithRange:[textCheckingResult rangeAtIndex:2]];

NSLog(@"http: '%@'", http);
NSLog(@"site: '%@'", site);

NSLog output:

http: 'http://link.com'
site: 'Link Name'
zaph
  • 111,848
  • 21
  • 189
  • 228
  • yes your right it works on some "siteName" but some are spaced as well like "Site Name" so I am running into another issue... but I can use some dirty coding to get around it if theres no other way – FreeAppl3 Oct 12 '11 at 21:29
  • 1
    You are probably better off using a Regular expression. – zaph Oct 12 '11 at 21:34
  • Thank you @CocoaFu I appreciate the help! For now I just used object at index 1 and last object to get what I needed but I will look into the Regular Expressions.. it seems more feasible. – FreeAppl3 Oct 12 '11 at 21:45
2

NSString has a method with the signature:

componentsSeparatedByString:

It returns an array of components as its result. Use it like this:

NSArray *components = [myNSString componentsSeparatedByString:@" "];

[components objectAtIndex:0]; //should be SiteName
[components objectAtIndex:1]; // should be http://Link.com

Good luck.

FreeAppl3
  • 858
  • 1
  • 15
  • 32
Dexter
  • 5,666
  • 6
  • 33
  • 45
  • Actually that won't get the site if there is more than one space character separation the components. – zaph Oct 12 '11 at 21:17
  • Thank you SO much! I knew it was a simple as a few lines of code just couldn't figure how to get them... both these answers will work! I appreciate the help immensely!!! – FreeAppl3 Oct 12 '11 at 21:21