简体   繁体   English

从字符串中删除元音

[英]Remove the vowels from a string

NSMutableString *stringa = [[NSMutableString alloc] initWithFormat:@"%@", surnameField.text];

if ([stringa length] < 3) {
    [stringa appendString:@"x"];
}

NSMutableString *consonanti = [[NSMutableString alloc] init];

NSCharacterSet *vocali = [NSCharacterSet characterSetWithCharactersInString:@"aeiouàèìòùáéíóúAEIOUÀÈÌÒÙÁÉÍÓÚ"];

NSRange r;

for (int i=0; i < [stringa length]; i++) {

    r = [stringa rangeOfCharacterFromSet:vocali];

    if (r.location != NSNotFound) {
        [consonanti appendFormat:@"%c",[stringa characterAtIndex:i]];
    }
    else {
    }
}

cfField.text = consonanti;
[stringa release];
[consonanti release];

The result of cfField.text is always consonants with vowels, while the result must be only consonants. cfField.text的结果总是与元音有辅音,而结果必须只是辅音。 I don't know. 我不知道。

You are testing for the presence of vowels in the whole string with each iteration of the loop, so you will always add each character in turn. 您正在测试循环的每次迭代时整个字符串中是否存在元音,因此您将始终依次添加每个字符。

In your for loop, you need the following code instead: 在for循环中,您需要以下代码:

if(![vocali characterIsMember:[stringa characterAtIndex:i]])
    [consonanti appendFormat:@"%C",[stringa characterAtIndex:i]];

This checks that the individual character is not in the vowel character set, and adds it to your mutable string. 这将检查单个字符是否不在元音字符集中,并将其添加到可变字符串中。

Notice that if you use characterAtIndex: to access the individual characters, composed characters will be broken into their single components, such as a diacritical mark. 请注意,如果使用characterAtIndex:来访问单个字符,则组合字符将被分解为单个组件,例如变音符号。

A diacritical mark in Unicode-speak is for instance an accent, like the one in "é" in your string of vowels. Unicode-speak中的变音符号例如是重音,就像你的元音串中的“é”中的重音一样。

A better way is to iterate the string over its composed characters: 更好的方法是将字符串迭代到其组合字符:

// A string with composed diacritic characters
// In clear text it is "Renée Ångström"
NSString *stringWithComposedChars = @"Rene\u0301e A\u030Angstro\u0308m";
NSString *vowels = @"aeiouàèìòùáéíóúäëïöü";

NSMutableString *consonants = [NSMutableString string];

[stringWithComposedChars
 enumerateSubstringsInRange:NSMakeRange(0,[stringWithComposedChars length])
                                        options:NSStringEnumerationByComposedCharacterSequences
                                        usingBlock: ^(NSString *substring,NSRange rng1, NSRange rng2, BOOL *stop)
{
    if ( [vowels rangeOfString:substring options:NSCaseInsensitiveSearch|NSWidthInsensitiveSearch].location == NSNotFound ) {
        [consonants appendString:substring];
    }
}];

NSLog(@"Original string: \"%@\" - Vowels removed: \"%@\"", stringWithComposedChars, consonants);

You will see that this snippet cleans the original string of composed characters for both the base vowel and the diacritical mark. 您将看到此片段清除基本元音和变音符号的原始字符串组合字符。

This should work too - first get rid of all vocals by using them as splitting characters for the string, then concatenate all received string parts again: 这也应该工作 - 首先通过使用它们作为字符串的拆分字符来摆脱所有人声,然后再次连接所有收到的字符串部分:

NSArray*  onlyConsonantsArray  = [stringa componentsSeparatedByCharactersInSet:vocali];
NSString* onlyConsonantsString = [onlyConsonantsArray componentsJoinedByString: @""];

I don't know about the performance, but it looks short :-). 我不知道性能,但它看起来很短:-)。

You could try something like: 你可以尝试类似的东西:

  -(NSString *) removeVowels:(NSString *) value
  {
      NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"([A,Á,Ã,E,É,Ê,I,Í,O,Ô,Ó,Õ,U,Û,Ü,Ú]?)" options:NSRegularExpressionCaseInsensitive error:nil];
      return [regex stringByReplacingMatchesInString:value options:0 range:NSMakeRange(0, [value length]) withTemplate:@""];
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM