2

I need an efficient piece of code that strips escape characters. This is regular escapes not HTML escape characters.

Example: "\"", "\\\\", "\", "\\"

I want a general algorithm to strip any kind of escape sequences.

Could use any utility like regular expression.

  (NSString*) unescape:(NSString*) string {

      ....
  }

This is the answer I wrote:

-(NSString*) unescape:(NSString*) string
{

    for(int i = 0; i < string.length; i++) {
        char a = [string characterAtIndex:i];
        if([string characterAtIndex:i] == '\\' ) {
            string = [string stringByReplacingCharactersInRange:NSMakeRange(i,1) withString:@""];
        }

    }
    return string;
}
user1307179
  • 233
  • 3
  • 10

6 Answers6

2

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:@"\""];
}
Artem Shmatkov
  • 1,434
  • 5
  • 22
  • 41
1

The best method that you should use is:

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

You would call it using:

string = [string stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
esreli
  • 4,993
  • 2
  • 26
  • 40
0

If its URL Encoded, you are probably looking for:

stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding
nalply
  • 26,770
  • 15
  • 78
  • 101
jeremy
  • 4,294
  • 3
  • 22
  • 36
0

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:@""];
iDev
  • 23,310
  • 7
  • 60
  • 85
0

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     
holex
  • 23,961
  • 7
  • 62
  • 76
-1

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:@"\\"];
    }
}
Yuliani Noriega
  • 1,085
  • 12
  • 23
  • No that's not all i want to do. I would have done that myself if it is that simple – user1307179 Nov 16 '12 at 21:55
  • that doesn't help. I need either a brute force way to scan every single characters from start to end... OR.. use regular expression.... so if you see "\\\\" you replace it with length of ("\\\\") divided by two number of \ characters..... "User regular expression to find/replace" – user1307179 Nov 16 '12 at 22:29