Objective-C: Find consonants in string

余生长醉 提交于 2019-12-29 09:03:37

问题


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

NSString *str = @"consonants.";

Result must be:

cnsnnts

回答1:


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

+ (id)characterSetWithCharactersInString:(NSString *)aString

then use the

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

method.

EDIT: 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.




回答2:


Unfortunately stringByTrimmingCharactersInSet wont work as it only trim leading and ending characters, but you could try using a regular expression and substitution like this:

[[NSRegularExpression
  regularExpressionWithPattern:@"[^bcdefghjklmnpqrstvwx]"
  options:NSRegularExpressionCaseInsensitive
  error:NULL]
 stringByReplacingMatchesInString:str
 options:0
 range:NSMakeRange(0, [str length])
 withTemplate:@""]

You probably want to tune the regex and options for your needs.




回答3:


Possible, for sure not-optimal, solution. I'm printing intermediate results for your learning. Take care of memory allocation (I didn't care). Hopefully someone will send you a better solution, but you can copy and paste this for the moment.

NSString *test = @"Try to get all consonants";
NSMutableString *found = [[NSMutableString alloc] init];
NSInteger loc = 0;
NSCharacterSet *consonants = [NSCharacterSet characterSetWithCharactersInString:@"bcdfghjklmnpqrstvwxyz"];
while(loc!=NSNotFound && loc<[test length]) {
    NSRange r = [[test lowercaseString] rangeOfCharacterFromSet:consonants options:0 range:NSMakeRange(loc, [test length]-loc)];
    if(r.location!=NSNotFound) {
        NSString *temp = [test substringWithRange:r];
        NSLog(@"Range: %@ Temp: %@",NSStringFromRange(r), temp);
        [found appendString:temp];
        loc=r.location+r.length;
    } else {
        loc=NSNotFound;
    }
}

NSLog(@"Found: %@",found);



回答4:


Here is a NSString category that does the job:

- (NSString *)consonants
{
    NSString *result = [NSString stringWithString:self];
    NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"aeiou"];

    while(1)
    {
        NSRange range = [result rangeOfCharacterFromSet:characterSet options:NSCaseInsensitiveSearch];
        if(range.location == NSNotFound)
            break;

        result = [result stringByReplacingCharactersInRange:range withString:@""];
    }


    return result;
}


来源:https://stackoverflow.com/questions/7859683/objective-c-find-consonants-in-string

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