Objective-C:在字符串中查找辅音 [英] Objective-C: Find consonants in string

查看:143
本文介绍了Objective-C:在字符串中查找辅音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,其中包含辅音和元音的单词。如何从字符串中仅提取辅音?

I have a string that contains words with consonants and vowels. How can I extract only consonants from the string?

NSString *str = @"consonants.";

结果必须是:

cnsnnts


推荐答案

你可以做包含所有元音的字符集(@aeiouy)

You could make a character set with all the vowels (@"aeiouy")

+ (id)characterSetWithCharactersInString:(NSString *)aString

然后使用

- (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet *)set

方法。

编辑:这只会删除其他帖子中指出的字符串开头和结尾的元音,你可以做的是使用

This will only remove vowels at the beginning and end of the string as pointed out in the other post, what you could do instead is use

- (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator

然后将组件重新组合在一起。您可能还需要在集合中包含元音的大写版本,如果您还想处理重音(例如等等),您可能还必须包括它。

then stick the components back together. You may also need to include capitalized versions of the vowels in the set, and if you want to also deal with accents (à á è è ê ì etc...) you'll probably have to include that also.

这篇关于Objective-C:在字符串中查找辅音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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