tags:

views:

4064

answers:

5

how do we pass a string Mr.X & Mr.Y in the URL.

I have tried this but this one does all the chars but ampersand.

[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]

Thanks for the help.

A: 

In a URL, the ampersand is a protected keyword signifying the inclusion of a querystring variable. You cannot put it in as part of the value itself. You need to change it to something else.

Here is a link to the same question asked on StackOverflow: http://stackoverflow.com/questions/561954/asp-net-urlencode-ampersand-for-use-in-query-string

Tacoman667
This is exactly the correct answer (at the link).
Kendall Helmstetter Gelner
...except it's for the wrong framework. No .NET on iPhone.
Roger Nolan
+1  A: 

Use %26 as url escape.

Other escapes:

$  %24
&  %26
+  %2B
,  %2C
/  %2F
:  %3A
;  %3B
=  %3D
?  %3F
@  %40
Gamecat
+7  A: 

-stringByAddingPercentEscapesUsingEncoding: does not perform complete escape encoding. You should manually add the encodings using -replaceOccurrencesOfString:withString:

Here's a complete list (mirroring Gamecat's list) as originally suggested at https://devforums.apple.com/message/15674#15674 .

NSMutableString *escaped = [actionString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
[escaped replaceOccurrencesOfString:@"&" withString:@"%26" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"+" withString:@"%2B" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"," withString:@"%2C" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"/" withString:@"%2F" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@":" withString:@"%3A" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@";" withString:@"%3B" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"=" withString:@"%3D" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"?" withString:@"%3F" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"@" withString:@"%40" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@" " withString:@"%20" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"\t" withString:@"%09" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"#" withString:@"%23" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"<" withString:@"%3C" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@">" withString:@"%3E" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"\"" withString:@"%22" options:NSCaseInsensitiveSearch range:wholeString];
[escaped replaceOccurrencesOfString:@"\n" withString:@"%0A" options:NSCaseInsensitiveSearch range:wholeString];

This code certainly makes a hash of your URL.

Roger Nolan
I believe the proper code would be more likeNSMutableString *escaped = [NSMutableString stringWithString:[actionString ...]];and NSMakeRange(0, [escaped length])] instead of wholeString.
Merlin
+10  A: 

Or even shorter:

@implementation NSString (Escaping)
- (NSString*)stringWithPercentEscape {            
    return [(NSString *) CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)[[self mutableCopy] autorelease], NULL, CFSTR("=,!$&'()*+;@?\n\"<>#\t :/"),kCFStringEncodingUTF8) autorelease];
}
@end
Holtwick
Shorter and scarier :)
bentford
Please see Roger's answer below. While this solution is a good general answer, it is not a complete URL encoding implementation as of iOS version 2.2. If anyone has any additional information about this being fixed in later iOS releases, please comment here.
Harkonian
A: 

Any one shall tell me the escape value for "%"

Rajendran