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:
- https://stackoverflow.com/a/5223737/700926
- https://stackoverflow.com/a/5217703/700926
- https://stackoverflow.com/a/714009/700926
- https://stackoverflow.com/a/668254/700926
- https://stackoverflow.com/a/2039396/700926
... 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.