NSStringSeparatedByString components "&" but ignore "& amp;"

You need to split the string into &, but not into &.

Is there a more elegant way to code this, and not just replace it and then split by &? Like this...

query = [query stringByReplacingOccurrencesOfString:@"&" withString:@"~~~"];
NSArray * kvpairs = [query componentsSeparatedByString:@"&"];

NSMutableArray *mArr = [[NSMutableArray alloc] init];
for (NSString *kvp in kvpairs) {
    [mArr addObject:[kvp stringByReplacingOccurrencesOfString:@"~~~" withString:@"&"]];
}
kvpairs = [NSArray arrayWithArray:mArr];
[mArr release];
+3
source share
3 answers

You can use NSRegularExpressionfor the transfer in a regular expression string which corresponds to &, but not &, for example @"&(?!amp;)". This will be more cumbersome than your current method, but more accurate, because it will work without changing the original string and will not rely on the value of the token.

, ~~~ , ~~~. , , .

+3

, 100% , ~~~ .

, . &, , amp;. , , , .

+1

HTML- URL-. , HTML- , "% 26" URL- .

However, you will run into problems if & is not the only HTML object or link, so if you are absolutely not sure, & is unique, you will need a more reliable solution.

0
source

All Articles