How to find and replace a particular unicode chara

2019-09-05 15:08发布

I am being provided a string that may have a unicode apostrophe in it (\u2019) and I must replace it with a standard apostrophe (plain old "'"). I usually use stringByReplacingOccurrencesOfString to do this kind of thing but trying this does not work:

sMyString = [sMyString stringByReplacingOccurrencesOfString:@"\\u2019" withString:@"'"];

Makes sense that it didn't work I guess since the actual values representing the apostrophe internally in the NSString will be numeric. Anyone know how to do this?

2条回答
爷的心禁止访问
2楼-- · 2019-09-05 15:55
sMyString = [sMyString stringByReplacingOccurrencesOfString:@"\U2019" withString:@"'"];

Do not use \U2019, it makes an error.

查看更多
Bombasti
3楼-- · 2019-09-05 16:11

If sMyString has the actual character then either do:

sMyString = [sMyString stringByReplacingOccurrencesOfString:@"\u2019" withString:@"'"];

or:

sMyString = [sMyString stringByReplacingOccurrencesOfString:@"’" withString:@"'"];

Both of these actually compile to the same code. The \u2019 is replaced by during compilation.

查看更多
登录 后发表回答