将特殊字符如ë,à,é,ä全部转换为e,a,e,a?目标C. [英] Convert special characters like ë,à,é,ä all to e,a,e,a? Objective C

查看:453
本文介绍了将特殊字符如ë,à,é,ä全部转换为e,a,e,a?目标C.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标c中有一个简单的方法,将ë,à,é,ä等所有特殊字符转换为正常字符,如e en a?

Is there a simple way in objective c to convert all special characters like ë,à,é,ä to the normal characters like e en a?

推荐答案

是的,它很简单:

NSString *src = @"Convert special characters like ë,à,é,ä all to e,a,e,a? Objective C";
NSData *temp = [src dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *dst = [[[NSString alloc] initWithData:temp encoding:NSASCIIStringEncoding] autorelease];
NSLog(@"converted: %@", dst);

在我的机器上运行该程序会产生:

Running that on my machine produces:

EmptyFoundation[69299:a0f] converted: Convert special characters like e,a,e,a all to e,a,e,a? Objective C

基本上,我们要求字符串转换自己 NSData (即,字节数组),表示ASCII字符集中的字符串中的字符。由于原始字符串中的所有字符都不是ASCII字符,因此我们告诉字符串,它可以做一个有损的转换。换句话说,可以打开é到e,等等。

Basically, we're asking the string to transform itself it an NSData (ie, a byte array) that represents the characters in the string in the ASCII character set. Since not all of the characters in the original string are in ASCII, we tell the string that it's OK to do a "lossy" conversion. In other words, it's OK to turn "é" into "e", and so on.

一旦我们得到了我们的字节数组,我们就把它转回一个字符串,我们完成了! :)

Once we've got our byte array, we simply turn it back into a string, and we're done! :)

这篇关于将特殊字符如ë,à,é,ä全部转换为e,a,e,a?目标C.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆