从字符串中删除元音

发布于 2024-12-12 05:59:51 字数 723 浏览 0 评论 0原文

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];

cfField.text 的结果始终是带有元音的辅音,而结果必须仅为辅音。我不知道。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱要勇敢去追 2024-12-19 05:59:51

您将在循环的每次迭代中测试整个字符串中是否存在元音,因此您始终会依次添加每个字符。

在 for 循环中,您需要以下代码:

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

这会检查单个字符是否不在元音字符集中,并将其添加到可变字符串中。

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.

亚希 2024-12-19 05:59:51

请注意,如果您使用 characterAtIndex: 访问单个字符,组合字符将被分解为单个组件,例如变音符号。

例如,Unicode 语言中的变音符号是一种重音符号,就像元音字符串中“é”中的重音符号一样。

更好的方法是在其组合字符上迭代字符串:

// 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);

您将看到此代码片段清除了原始字符串中的基本元音和变音符号的组合字符。

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.

空城之時有危險 2024-12-19 05:59:51

这也应该有效 - 首先通过使用它们作为字符串的分割字符来消除所有声音,然后再次连接所有收到的字符串部分:

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

我不知道性能如何,但它看起来很短:-)。

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

海拔太高太耀眼 2024-12-19 05:59:51

你可以尝试这样的事情:

  -(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:@""];
  }

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:@""];
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文