Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

printf

macrumors regular
Original poster
Aug 27, 2008
105
0
string parsing has never been my forte, and the few results i've found googling are a bit unwieldy and not malleable enough for any future custom revisions i might want to make.

are there any simple/elegant ways to pull out the individual parameters for inspection?

for example, if i want to get the values of a, b, and c:
http://www.mydomain.com/?a=1&b=2&c=3
 

printf

macrumors regular
Original poster
Aug 27, 2008
105
0
btw, the solution can use std string, cstring, nsstring... doesn't matter to me.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,751
8,425
A sea of green
Do you need URL-decoding on the parameters? If so, you'll have to apply it.


This seems like a basic problem in decomposition or factoring.

Break it down into smaller sub-problems, until you have solvable sub-problems. It's fundamental to programming, or almost any other engineering or technical problem-solving field:
http://en.wikipedia.org/wiki/Decomposition_(computer_science)


You could start by creating an NSURL, then calling its query method. That should return the "a=1&b=2&c=3" part. You've now separated the part you want from the part you don't care about.

Or you could find the first "?" in the URL string and take everything after that point. There are no unescaped ?'s in a URL, so you shouldn't have to worry about that. Again, this is simply identifying and separating the part you want from the part you don't care about.


With the NSString containing "a=1&b=2&c=3", you can use NSString's componentsSeparatedByCharactersInSet method, with a character-set containing only the '&'. This will get you an NSArray of NSStrings, each one being a "a=1", "b=2", etc. Decomposition again.

For each string in the array, split it at the '=' into a name and a value. More decomposition.

For each split string, you could use the first part as a dictionary key, the second part as a dictionary value, and add them to a dictionary. Then you can repeatedly find values by their name (key) without having to repeat the decompositions.


If that's not malleable enough, then you'll have to describe what kind of malleability you want. No one has any way to guess your specifications. You have to specify them.
 

Luke Redpath

macrumors 6502a
Nov 9, 2007
733
6
Colchester, UK
I wouldn't bother with a regex.

You can extract the query string by creating an NSURL object from the string and using the query property.

You can then just split it on & to get the key pairs and each key pair by = to get the key and value. That should generally do the trick.

Edit: I just remembered that I have some categories that I use in a few of my projects that you are welcome to use. http://github.com/lukeredpath/LRResty/tree/master/Categories/

There are methods for getting a dictionary from a query string and back again with support for escaping and nested parameters.
 

printf

macrumors regular
Original poster
Aug 27, 2008
105
0
With the NSString containing "a=1&b=2&c=3", you can use NSString's componentsSeparatedByCharactersInSet method, with a character-set containing only the '&'. This will get you an NSArray of NSStrings, each one being a "a=1", "b=2", etc. Decomposition again.

For each string in the array, split it at the '=' into a name and a value. More decomposition.

For each split string, you could use the first part as a dictionary key, the second part as a dictionary value, and add them to a dictionary. Then you can repeatedly find values by their name (key) without having to repeat the decompositions.

yeah, this is kind of what i'm after. guess i wasn't sure if there was a more 'out of the box solution' for this type of object. in javascript, i'd split the '&', then the '=' and call it a day, but it's obviously more complicated in c, and i'm still newish to parsing strings in obj-c.


Edit: I just remembered that I have some categories that I use in a few of my projects that you are welcome to use. http://github.com/lukeredpath/LRResty/tree/master/Categories/

There are methods for getting a dictionary from a query string and back again with support for escaping and nested parameters.

I'll take a look, thx!
 

bonatto

macrumors newbie
Jan 27, 2011
1
0
This was my tentative of finding a relatively simple way to parse a query string.

Code:
NSArray *parameters = [[url query] componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"=&"]];
NSMutableDictionary *keyValueParm = [NSMutableDictionary dictionary];
		
for (int i = 0; i < [parameters count]; i=i+2) {
	[keyValueParm setObject:[parameters objectAtIndex:i+1] forKey:[parameters objectAtIndex:i]];
}

Hope it helps.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.