DAS
DAS

Reputation: 1941

Format NSString (Char-Replacement, UTF, ...)

Google Maps API delivers me a string which contains the German letters: ö, ä , ü and probably several other special characters.

The string looks like:

@" (several spaces ...) Frankfurt an der Oder (several spaces ...) "

(1) If I try stringByReplacing ... and make the spaces disappear, it looks like:

@"FrankfurtanderOder" ... which is even worse. So I need to delete the spaces before the first and after the last word, not the spaces in between. How to do this?

(2) Sometimes Google delivers me @"W\U00fcrzburg, Deutschland"

... there is nothing said in the JSON-request about encodings ... could it be that the JSON-parser and not the api is the problem?

However, still I have to solve it. Any ideas?

Thank you so far!

EDIT:

For (2) I'll do the workaround and replace some UTF-8 characters ... (Even If this is definitely not the best solution ...)

ä -> ä
ö -> ö
ü -> ü
Ä -> Ä
Ö -> Ö
Ü -> Ü
ß -> ß
" -> "
\u00C4 -> Ä
\u00E4 -> ä
\u00D6 -> Ö
\u00F6 -> ö
\u00DC -> Ü
\u00FC -> ü
\u00DF -> ß

Upvotes: 0

Views: 1125

Answers (2)

Richard J. Ross III
Richard J. Ross III

Reputation: 55573

You need a few steps here:

NSString *unescapeBackslashes(NSString *input)
{
    // find occurences of '\'
    int index = 0;
    NSRange range = NSMakeRange(0, input.length);
    NSMutableString *output = [NSMutableString string];

    while ((range = [input rangeOfString:@"\\u" options:0 range:NSMakeRange(index, input.length - index)]).location != NSNotFound) {
        assert(input.length > range.location + 5);

        char temp[5];
        strncpy(temp, [input cStringUsingEncoding:NSASCIIStringEncoding] + range.location + 2, 4);

        [output appendString:[input substringWithRange:NSMakeRange(index, range.location - index)]];

        // append the unicode char
        [output appendFormat:@"%C", strtol(temp, NULL, 16)];

        index = range.location + 6;
    }

    [output appendString:[input substringWithRange:NSMakeRange(index, input.length - index)]];

    return output;
}

int main(int argc, const char *argv[])
{ 
    @autoreleasepool {
        NSString *input = @"    W\\u00fcrzburg, Deutschland    ";
        NSLog(@"Input: %@", input);

        NSString *trimmed = [input stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        NSString *escaped = unescapeBackslashes(trimmed);

        NSLog(@"Trimmed: %@", trimmed);
        NSLog(@"Escaped: %@", escaped);
    }
}

Upvotes: 0

Matthias Bauch
Matthias Bauch

Reputation: 90117

– stringByTrimmingCharactersInSet:

NSString *str = @"  Frankfurt an der Oder   ";
NSString *trimmed = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"\"%@\"", str);
NSLog(@"\"%@\"", trimmed);

2012-03-26 14:10:49.302 xx[3752:f803] "  Frankfurt an der Oder   "
2012-03-26 14:10:49.333 xx[3752:f803] "Frankfurt an der Oder"

about the ü. Does the \U00fc appear in an UILabel or did you just got them from a NSLog? In my experience sometimes NSLog doesn't print the decoded letters but they appear okay in interface elements.

Upvotes: 3

Related Questions