Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove '\' from the string in IOS application

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.

like image 613
kumar Sudheer Avatar asked Jul 24 '26 21:07

kumar Sudheer


1 Answers

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];
like image 130
Midhun MP Avatar answered Jul 26 '26 12:07

Midhun MP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!