Lasse Christiansen
Lasse Christiansen

Reputation: 10325

What is the right way to replace a given unicode char in an NSString instance?

I have an NSString instance (let's called it myString) containing the following UTF-8 unicode character: \xc2\x96 ( that is the long dash seen in, e.g., MS Word ).

When printing the NSString to the console using NSLog and the %@ format specifier, the character is replaced by an upside-down question mark indicating that something is wrong - and when using it as text in a table cell, the unicode character simply appears as blank space ( not the empty string - a blank space ).

To solve this, I would like to replace the \xc2\x96 unicode character with a "normal" dash - at first I thought this should be a 10 sec. task but after some research I have not yet found the "right way" to do this and this is where I would like your help.

What I have tried:

When I print myString in hex like this NSLog(@"%x", myString) I get the hex value: 96 for the unicode character representing the unicode character \xc2\x96.

Using this information I have made the following implementation to replace it with its "normal" dash equivalent:

for(int index = 0; index < [myString length]; index++)
{
    NSLog(@"Hex:'%x' Char:'%c'", [myString characterAtIndex:index],[myString characterAtIndex:index]);

    if([[NSString stringWithFormat:@"%x", [myString characterAtIndex:index]] isEqualToString:@"96"])
        myString = [myString stringByReplacingCharactersInRange:NSMakeRange(index, 1) withString:@"-"];
}

... it works, but my eyes don't like it, and I would like to know if this can be done in much more cleaner and "right" way? E.g. like C#'s String.Replace(char,char) which supports unicode characters .

So to wrap up:

I'm looking for the "right way" to replace unicode chars in a string - I have done some research, but apparently, there is only methods available that replaces occurrences of a given NSString with another NSString.

I have read the following:

... but all of them explains how to replace a given NSString with another NSString and do not cover how specific unicode characters ( in particular double byte ) can be replaced.

Upvotes: 1

Views: 1965

Answers (1)

user529758
user529758

Reputation:

You can make your string mutable (i. e. use an NSMutableString instead of an NSString). Also, the call to [[NSString stringWithFormat:@"%x", character] isEqualToString:@"96"] is as inefficient as possible - why not simply if (character == 0x96)? All in all, try

NSString *longDash = @"\xc2\x96";
[string replaceOccurrencesOfString:longDash withString:@"-"];

Upvotes: 1

Related Questions