-1

I'm doing this to encode my URL in this way, but its not working, i got the result in NSLog but its the same url nothing is changing.

Please help me to sort this issue.

below is my code :

 NSString *unencodedUrlString = 

[@"http://www.demii.com/demo/dooponz/admin/index.php/chat/new_message/4/1/you/2,7" 

stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSLog(@" %@", unencodedUrlString);

Thanks in advance

Lithu T.V
  • 19,955
  • 12
  • 56
  • 101
zeeshan shaikh
  • 819
  • 3
  • 18
  • 33
  • 3
    so actually what do you expect as output? – Lithu T.V Jun 11 '13 at 09:34
  • @LithuT.V i expect this output:- http://www.demii.com/demo/dooponz/admin/index.php/chat/new_message/4/1/you/2%2C7 – zeeshan shaikh Jun 11 '13 at 09:38
  • http://stackoverflow.com/questions/3140424/stringbyaddingpercentescapesusingencoding-not-working-with-nsstrings-with-0 ; https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html Seems to me that you understand the purpose of *stringByAddingPercentEscapesUsingEncoding* in a wrong way – makaron Jun 11 '13 at 09:43

2 Answers2

2

The comma is a legal URL character, therefore stringByAddingPercentEscapesUsingEncoding leaves "2,7" as it is and does not replace it by "2%2C7".

If you want the comma to be replaced by a percent escape (as I understand from your comment to the question), you can use CFURLCreateStringByAddingPercentEscapes instead:

NSString *str = @"http://www.demii.com/demo/dooponz/admin/index.php/chat/new_message/4/1/you/2,7";
NSString *encoded = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                (__bridge CFStringRef)(str), NULL, CFSTR(","), kCFStringEncodingUTF8));
NSLog(@"%@", encoded);

Output:

http://www.demii.com/demo/dooponz/admin/index.php/chat/new_message/4/1/you/2%2C7

The fourth parameter CFSTR(",") specifies that the comma should be replaced by a percent escape even if it is a legal URL character.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
0

Use this

    NSString *str = [NSString stringWithFormat:@"http://www.demii.com/demo/dooponz/admin/index.php/chat/new_message/4/1/you/2,7"];
    NSString *path = [str stringByReplacingOccurrencesOfString:@"," withString:@"/"];
    NSLog(@"%@",path);

This will do nothing but will make , to /.

Lithu T.V
  • 19,955
  • 12
  • 56
  • 101