Reputation: 3768
I have a URL and want to extract part of the string.
The URL is in a similar format to this: www.google.com?id=10&jkhsds=fg.php.
I want to extract the value of id. How would I do this?
Upvotes: 0
Views: 300
Reputation: 6805
If you create an NSURL
object, like so:
NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
You can use the methods of the NSURL
object to get whatever you need, e.g. scheme
, host
, port
, path
, and query
.
See here:
Upvotes: 2
Reputation: 2992
You might be able to get exactly what you want from various properties of the NSURL class such as baseURL,host,path,query etc...
If your URL is an NSString you can create an NSURL with [NSURL URLWithString:string]
Upvotes: 0
Reputation: 1617
You can access the string through the absoluteString
property of the NSURL
class. Then you can extract some parts from the string with methods like
- (NSString *)substringFromIndex:(NSUInteger)anIndex;
- (NSString *)substringToIndex:(NSUInteger)anIndex;
- (NSString *)substringWithRange:(NSRange)aRange;
Upvotes: 0
Reputation: 27295
See the NSString class reference. Methods which are likely to be of interest to you include rangeOfString, componentsSeparatedByString, substringFromIndex, and substringToIndex
Upvotes: 1