1
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. I don't know.

swiftBoy
  • 35,607
  • 26
  • 136
  • 135
Mario
  • 17
  • 1
  • 7
  • What are you trying to do? Remove the vowels from a string? – jrturton Oct 28 '11 at 22:14
  • Exact duplicate of http://stackoverflow.com/questions/7859683/objective-c-find-consonants-in-string – rob mayoff Oct 28 '11 at 22:21
  • possible duplicate of [How-to articles for iPhone development and Objective-C](http://stackoverflow.com/questions/1939/how-to-articles-for-iphone-development-and-objective-c) – nos Oct 29 '11 at 18:04

4 Answers4

3

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:

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.

jrturton
  • 118,105
  • 32
  • 252
  • 268
1

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.

A diacritical mark in Unicode-speak is for instance an accent, like the one in "é" in your string of vowels.

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.

Monolo
  • 18,205
  • 17
  • 69
  • 103
0

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

TheEye
  • 9,280
  • 2
  • 42
  • 58
0

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:@""];
  }
gabriel
  • 2,351
  • 4
  • 20
  • 23