How to find and replace a particular unicode character with a non-unicode character in an NSString?

杀马特。学长 韩版系。学妹 提交于 2019-12-25 10:01:25

问题


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?


回答1:


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.




回答2:


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

Do not use \U2019, it makes an error.



来源:https://stackoverflow.com/questions/35304347/how-to-find-and-replace-a-particular-unicode-character-with-a-non-unicode-charac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!