-2

This is my string.

NSString *str=@"A & B";

now i am converting it to NSUTF8StringEncoding.

str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"str: %@", str);

space is replaced with %20 but & is not replaced with %26.

nslog show

str: A%20&%20B

This is also not working

NSString *str=@"(A) & (B)";
str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"str: %@", str);

nslog show

str: (A)%20&%20(B)

I need this because i have to pass this as parameter value in webservice. Anybody have idea for this. Please help me for this issue. Nice answer will be appreciated

Mike Abdullah
  • 14,933
  • 2
  • 50
  • 75
Viral Narshana
  • 1,855
  • 2
  • 21
  • 45
  • 1
    That process is called *URL Encoding*, and has nothing to do with UTF-8. I also didn't get what your actual question was? – trojanfoe Feb 28 '13 at 10:00
  • Thanks for answering trojanfoe. Yes, i acctuly want the url encoding. I mentioned that "I need this because i have to pass this as parameter value in webservice." – Viral Narshana Feb 28 '13 at 10:24
  • And yet the title of your question doesn't mention it and you don't actually say what the issue is you are having. – trojanfoe Feb 28 '13 at 10:25
  • Yes, you are most definitely *not* "converting it to NSUTF8StringEncoding". You are adding percent escapes, using UTF-8 for *their* encoding – Mike Abdullah Apr 09 '13 at 14:52

1 Answers1

3

You can use CFURLCreateStringByAddingPercentEscapes() to do that:

NSString *string = ...;
NSString *encodedString = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef)string, NULL, CFSTR("!*'();:@&=+$,/?%#[]"), kCFStringEncodingUTF8));
Fabian Kreiser
  • 8,307
  • 1
  • 34
  • 60