Remove '\' from the string in IOS application

kumar Sudheer picture kumar Sudheer · Mar 8, 2013 · Viewed 7.7k times · Source

I am getting a url as string from json response with backslash character. I want to remove the '\' character from the url. I build up the code but it is not working. The code is here:

 NSString *responseData = [[NSString alloc]initWithData:[NSData dataWithContentsOfURL:url] encoding:NSUTF8StringEncoding];
    NSString* encodedString = [responseData stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSLog(@"Response ==> %@" ,encodedString);

       // here encodedString is a url which is showing crctly in output and need to trim the \ character.

       // NSString* str = [NSString stringWithFormat: @"encodedString"];
       // str = [str stringByReplacingOccurrencesOfString:@"?" withString:@""];
       // NSLog(@"Response ==> %@" , str);

    encodedString = [encodedString stringByReplacingOccurrencesOfString:@"\\" withString:@""];
        NSLog(@"Response ==> %@" ,encodedString);

but I am not able to remove the characters.

Answer

Midhun MP picture Midhun MP · Mar 8, 2013

If you want to remove the spaces and \ character, it is better to remove them from responseData.

NSString *responseData = [[NSString alloc]initWithData:[NSData dataWithContentsOfURL:url] encoding:NSUTF8StringEncoding];

responseData = [responseData stringByReplacingOccurrencesOfString:@" " withString:@""];
responseData = [responseData stringByReplacingOccurrencesOfString:@"\\" withString:@""];
NSString* encodedString = [responseData stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];