Question

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 !

Was it helpful?

Solution

@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];

OTHER TIPS

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'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top