NSString strip regular \\ escape characters how?

人盡茶涼 提交于 2019-12-06 13:14:48

Try to use below code for HTML escape

(NSString*) unescape:(NSString*) string 
{
    return [string stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}

for regular Escape

(NSString*) unescape:(NSString*) string 
{    
    return [string stringByReplacingOccurrencesOfString:@"\\\"" withString:@"\""];
}

The best method that you should use is:

- (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet *)set

You would call it using:

string = [string stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];

If you want to use regex, you can try with regex pattern \\[bntr\\\\"]. Or use any required regex pattern here.

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:
                              @"\\[bntr\\\\"]" options:0 error:nil];

[regex replaceMatchesInString:str options:0 range:NSMakeRange(0, [str length]) withTemplate:@""];

I apoligize for my answer...

NSString *_string = @"\\\\ dfsdg \\ tr\\\\t \\\\\\tw\\\\\\\\w\\ t\\ \\\\ \\ \\\\\\  rret\\    \\\\ \\\\\\\\";
NSLog(@"%@", [_string stringByReplacingOccurrencesOfString:@"\\" withString:@""]);

result is:

 dfsdg  trt tww t     rret     
jeremy

If its URL Encoded, you are probably looking for:

stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding

Hope this helps:

-(NSString *)unescape:(NSString *)string 
{
    if ([string rangeOfString:@"\\"].location != NSNotFound)
    {
        [string stringByReplacingCharactersInRange:@"\\" withString:@"\\"];
    }
    else if ([string rangeOfString:@"\\\\"].location != NSNotFound)
    {
        [string stringByReplacingCharactersInRange:@"\\\\" withString:@"\\"];
    }
    else if ([string rangeOfString:@"\\\\"].location != NSNotFound)
    {
        [string stringByReplacingCharactersInRange:@"\\\\" withString:@"\\"];
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!