iosnsstringmkmapviewmapkitmkplacemark

Reformat the string result from placemark


When i search a location sometimes i get this string from placemark.subThoroughfare how can i reformat that sign to a normal "-"?

enter image description here

Best thanks for the answers


Solution

  • Assuming that the problem is En Dash or Em Dash characters here is an example resolution. If your character is different look at the UTF8 data and use that instead.

    NSString *enDashCharacter = [NSString stringWithUTF8String:"\xe2\x80\x93"];
    NSString *emDashCharacter = [NSString stringWithUTF8String:"\xe2\x80\x94"];
    NSLog(@"enDashCharacter: %@", enDashCharacter);
    NSLog(@"emDashCharacter: %@", emDashCharacter);
    
    NSString *textString = [NSString stringWithFormat:@"1-2 3%@4 5%@6", enDashCharacter,emDashCharacter];
    NSData *textData = [textString dataUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"textString: '%@'", textString);
    NSLog(@"textData: %@", textData);
    
    NSString *newString = textString;
    newString = [newString stringByReplacingOccurrencesOfString:enDashCharacter withString:@"-"];
    newString = [newString stringByReplacingOccurrencesOfString:emDashCharacter withString:@"-"];
    NSLog(@"newString:  '%@'", newString);
    
    NSData *newStringData = [newString dataUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"newStringData: %@", newStringData);
    

    NSLog output:
    enDashCharacter: –
    emDashCharacter: —
    textString: ' 1 - 2 3 – 4 5 — 6 '
    textData: <2031202d 20322033 20e28093 20342035 20e28094 203620>
    newString: ' 1 - 2 3 - 4 5 - 6 '
    newStringData: <2031202d 20322033 202d2034 2035202d 203620>