0

I have a string formatted like this : Oy\U00e9\U00e9 Oy\U00e9 So, I found something in some forum and tried to adapt it. This is what I use :

// To keep whiteSpaces safe
string = [string stringByReplacingOccurrencesOfString:@" " withString:@"@@**@@"];
NSMutableString* clean_string = [NSMutableString string];
NSScanner* scanner = [NSScanner scannerWithString:string];
NSString* buf = nil;

while (![scanner isAtEnd] ) {
    //Plus de caractères à scanner
    if (![scanner scanUpToString: @"\\u" intoString: &buf] ){
        break;
    }
    NSLog(@"%@", buf);
    [clean_string appendString: buf];

    //Fin du scan
    if ([scanner isAtEnd] ){
        break;
    }

    [scanner setScanLocation: [scanner scanLocation] + 3];//skip the '\\u'
    unsigned c = 0;

    if ([scanner scanHexInt: &c]){
        [clean_string appendFormat: @"%c", c];
    }else{
        [clean_string appendString: @"\\u"];//nm
    }
}



self.cleanStringWithSpaces= [clean_string stringByReplacingOccurrencesOfString:@"@@**@@" withString:@" "];

return self.cleanStringWithSpaces;

The problem is that when I have a string with 2 \u00e9 following, the string is cut.

Example:

Oy\U00e9\U00e9 Oy\U00e9    Give ====> Oyé

Instead of : Oyéé Oyé

Maybe I missed something ... Hope you could help me !

Have a nice day !

Bhanu
  • 1,249
  • 10
  • 17
Ritooon
  • 155
  • 12
  • http://stackoverflow.com/questions/594797/how-to-use-nsscanner – iPatel Apr 03 '14 at 14:54
  • 1
    Are you *certain* your string contains `\Uxxxx` and that's not just how you are seeing it in logs/etc.? – trojanfoe Apr 03 '14 at 14:55
  • Yes I get this like this. I'm sure, but do you know a method to know it without logs ? – Ritooon Apr 03 '14 at 15:00
  • @iPatel, not really what I'm looking for. – Ritooon Apr 03 '14 at 15:03
  • 1
    Why does your code scan for lowercase "u" when your input string has uppercase "U"? – Martin R Apr 03 '14 at 17:11
  • This does not address the code you're trying to use, but the original problem: you might find either [Converting escaped UTF-8 characters back to their original form](http://stackoverflow.com/q/7860867) or [Removing Unicode and backslash escapes from NSString converted from NSData](http://stackoverflow.com/q/9018347) useful. – jscs Apr 03 '14 at 18:14
  • @JoshCaswell Thank you very much ! The first link made it ! A lot of thanks to you ! You could post it in an answer, I'll validate it ;) – Ritooon Apr 04 '14 at 08:14

2 Answers2

1

@JoshCaswell made my day by answer to my question.

I used something I found here : Converting escaped UTF8 characters back to their original form

[NSString stringWithCString:[name2escaped cStringUsingEncoding:NSUTF8StringEncoding]
        encoding:NSNonLossyASCIIStringEncoding];
Community
  • 1
  • 1
Ritooon
  • 155
  • 12
0

I think the problem is with the line:

[scanner setScanLocation: [scanner scanLocation] + 3];//skip the '\\u'

When changing the scanners location, you are counting the escape character. @"\\u" is actually only \u, therefore you should use:

[scanner setScanLocation: [scanner scanLocation] + 2];//skip the '\\u'
Daniel
  • 20,420
  • 10
  • 92
  • 149