I need to get url string from NSString. I do the following:
NSString * str = [NSString stringWithString:@"[email protected]"];
NSString * eStr = [str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@",eStr);
The result is [email protected]. But I need i%40gmail.com. replacing NSUTF8StringEncoding with NSASCIIStringEncoding doesn't help.
You're using the wrong method. This does the opposite, translating percent
escapes to their characters. You probably want to use
stringByAddingPercentEscapesUsingEncoding:
.
NSString *str = @"[email protected]";
NSString *eStr =
[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Apart from that, looks like the @
character is not escaped by default. Then,
as the documentation for the above method points out, you'll need to use
CoreFoundation to achieve what you want.
NSString *str = @"[email protected]";
CFStringRef eStr = CFURLCreateStringByAddingPercentEscapes(
kCFAllocatorDefault,
(CFStringRef)str,
NULL,
(CFStringRef)@"@",
kCFStringEncodingUTF8
);
NSLog(@"%@", eStr);
CFRelease(eStr);
Please check the documentation to know more about the function used and how to make it fit your needs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With