Linux world
Linux world

Reputation: 3768

How to extract a value of a string which is there in url

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

Answers (4)

Josh Hinman
Josh Hinman

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:

http://developer.apple.com/library/mac/#documentation/cocoa/reference/Foundation/Classes/NSURL_Class/Reference/Reference.html

Upvotes: 2

Ben
Ben

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

brutella
brutella

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

William Jockusch
William Jockusch

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

Related Questions