【问题标题】:Append string to mutablestring将字符串附加到可变字符串
【发布时间】:2011-10-24 15:16:56
【问题描述】:
NSString *str= @"surname";
NSMutableString *consonants = [[NSMutableString alloc] init];
NSMutableString *vowels = [[NSMutableString alloc] init];

for (int i=0; i < [str length]; i++){
    if ([str characterAtIndex:i] != 'a' && [str characterAtIndex:i] != 'e' && [str characterAtIndex:i] != 'i' && [str characterAtIndex:i] != 'o' && [str characterAtIndex:i] != 'u') {
            [consonants appendFormat:@"%c",[str characterAtIndex:i]];
        }
        else{
            [vowels appendFormat:@"%c",[str characterAtIndex:i]];
        }
    }


 if([consonants length] < 3){
      [consonants appendFormat:@"%@", [vocali characterAtIndex:1]];
 }

我的问题如下:
如果辅音少于 3 个,我必须将 n 个元音附加到辅音字符串。

示例:
str = "马里奥";
辅音=“mra”; // 2个辅音和1个元音

str = 狮子座;
辅音=“狮子座”; // 1 个辅音和 2 个元音

想。

【问题讨论】:

  • 我已经阅读并阅读了您的问题,但我只是不明白您想要什么

标签: iphone objective-c nsmutablestring


【解决方案1】:

如果您知道输入字符串将大于或等于 3,则可以使用 while 循环:

int i = 0;
while([consonant length]<3){
   [consonant appendFormat:@"%c",[vocali characterAtIndex:i]];
   i++
}

【讨论】:

  • 如果这回答了问题,请检查左侧的复选标记。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-14
  • 2012-05-28
  • 2012-10-04
  • 2012-01-12
  • 2012-09-14
  • 2012-09-09
  • 1970-01-01
相关资源
最近更新 更多