7

I have an string which is got from parsing an xml site.

http://www.arijasoft.com/givemesomthing.php?a=3434&b=435edsf&c=500

I want to have an NSString function that will be able to parse the value of c. Is there a default function or do i have to write it manually.

Nareshkumar
  • 2,331
  • 2
  • 20
  • 30

4 Answers4

22

You could use Regular expression via RegExKit Lite: http://regexkit.sourceforge.net/RegexKitLite/

Or you could separate the string into components (which is less nice):

NSString *url=@"http://www.arijasoft.com/givemesomthing.php?a=3434&b=435edsf&c=500";
NSArray *comp1 = [url componentsSeparatedByString:@"?"];
NSString *query = [comp1 lastObject];
NSArray *queryElements = [query componentsSeparatedByString:@"&"];
for (NSString *element in queryElements) {
    NSArray *keyVal = [element componentsSeparatedByString:@"="];
    if (keyVal.count > 0) {
       NSString *variableKey = [keyVal objectAtIndex:0];
       NSString *value = (keyVal.count == 2) ? [keyVal lastObject] : nil;
    }
}
Felix Lamouroux
  • 7,414
  • 29
  • 46
  • Thanks.. This is what i am trying to use. – Nareshkumar Feb 11 '10 at 14:19
  • 1
    this works until parameter value contains the '=' char, like LptOaQ== For that use instead `NSString *value = [element substringFromIndex:[element rangeOfString:@"="].location+1]; NSString *variableKey = [element substringToIndex:[element rangeOfString:@"="].location];` – jold Jan 21 '15 at 13:53
  • NSString *query = [url query]; is a bit cleaner to use then separating by "?" ... if the lastObject contains a fragment ("#fragment"), the last object when separating by "&" wouldn't be what you're expecting. – whyoz Sep 10 '15 at 20:42
6

I made a class that does this parsing for you using an NSScanner, as an answer to the same question a few days ago. You might find it useful.

You can easily use it like:

    URLParser *parser = [[[URLParser alloc] initWithURLString:@"http://www.arijasoft.com/givemesomthing.php?a=3434&b=435edsf&c=500"] autorelease];
    NSString *c = [parser valueForVariable:@"c"];   //c=500
Community
  • 1
  • 1
Dimitris
  • 13,480
  • 17
  • 74
  • 94
0

Try the following:

NSURL *url = [NSURL URLWithString:@"http://www.arijasoft.com/givemesomthing.php?a=3434&b=435edsf&c=500"];
NSMutableString *parameterString = [NSMutableString stringWithFormat:@"{%@;}",[url parameterString]];
[parameterString replaceOccurrencesOfString:@"&" withString:@";"];
// Convert string into Dictionary
NSPropertyListFormat format;
NSString *error;    
NSDictionary *paramDict = [NSPropertyListSerialization propertyListFromData:[parameterString dataUsingEncoding:NSUTF8StringEncoding] mutabilityOption: NSPropertyListImmutable format:&format errorDescription:&error];

// Now take the parameter you want
NSString *value = [paramDict valueForKey:@"c"];
diederikh
  • 25,221
  • 5
  • 36
  • 49
0

Here is the native iOS approach using NSURLComponents and NSURLQueryItem classes:

NSString *theURLString = @"http://www.arijasoft.com/givemesomthing.php?a=3434&b=435edsf&c=500";    
NSArray<NSURLQueryItem *> *theQueryItemsArray = [NSURLComponents componentsWithString:theURLString].queryItems;
for (NSURLQueryItem *theQueryItem in theQueryItemsArray)
{
    NSLog(@"%@ %@", theQueryItem.name, theQueryItem.value);
}
OlDor
  • 1,460
  • 12
  • 18