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=f开发者_如何转开发g.php.
I want to extract the value of id. How would I do this?
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
See the NSString class reference. Methods which are likely to be of interest to you include rangeOfString, componentsSeparatedByString, substringFromIndex, and substringToIndex
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;
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]
精彩评论