本文介绍了NSString - 如何从“ÁlgeBra”到“代数”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有人知道锄头会得到一个像ÁlgeBra到代数的NSString,没有重音,并只大写首字母?
p>
RL
解决方案
NSString
有一个名为:
NSString * str = @AlgeBra
NSString * other = [str capitalizedString];
NSLog(@Old:%@,New:%@,str,other);
编辑:
也想删除重音。您可以通过一系列步骤:
//原始字符串
NSString * str = @ÁlgeBra
//转换为数据对象,使用有损的转换为ASCII
NSData * asciiEncoded = [str dataUsingEncoding:NSASCIIStringEncoding
allowLossyConversion:YES];
//获取数据对象并使用有损转换重建一个字符串
NSString * other = [[NSString alloc] initWithData:asciiEncoded
encoding:NSASCIIStringEncoding];
//放弃所有权
[其他autorelease];
//创建最终大写字符串
NSString * final = [other capitalizedString];
明确表示,信件‘Á&rsquo ;将转换为‘ A’当转换为ASCII。
Does anyone knows hoe to get a NSString like "ÁlgeBra" to "Algebra", without the accent, and capitalize only the first letter?
Thanks,
RL
解决方案
NSString
has a method called capitalizedString
:
NSString *str = @"AlgeBra";
NSString *other = [str capitalizedString];
NSLog (@"Old: %@, New: %@", str, other);
Edit:
Just saw that you would like to remove accents as well. You can go through a series of steps:
// original string
NSString *str = @"ÁlgeBra";
// convert to a data object, using a lossy conversion to ASCII
NSData *asciiEncoded = [str dataUsingEncoding:NSASCIIStringEncoding
allowLossyConversion:YES];
// take the data object and recreate a string using the lossy conversion
NSString *other = [[NSString alloc] initWithData:asciiEncoded
encoding:NSASCIIStringEncoding];
// relinquish ownership
[other autorelease];
// create final capitalized string
NSString *final = [other capitalizedString];
The documentation for dataUsingEncoding:allowLossyConversion:
explicitly says that the letter ‘Á’ will convert to ‘A’ when converting to ASCII.
这篇关于NSString - 如何从“ÁlgeBra”到“代数”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!