【问题标题】:How to get the first charecters from array of strings in case insensitive如何在不区分大小写的情况下从字符串数组中获取第一个字符
【发布时间】:2012-10-31 14:23:44
【问题描述】:

在我的 iPhone 应用中,我有一系列的刺痛。

从那个字符串我需要获取第一个字符的数组。为此,我实现了如下逻辑。它有效,但在此我已经检索了 twicw 1.Small 字母和 2.Capital 字母的字符。

即使是小写字母,我也只需要大写字母。

like Only "A" for America, Africa and america, antantica

contactIndex = [[NSMutableArray alloc] init];

      for (int i=0; i<[contactsArray count]-1; i++){
            //---get the first char of each state---
            char alphabet = [[contactsArray objectAtIndex:i] characterAtIndex:0];
            NSString *uniChar = [NSString stringWithFormat:@"%C", alphabet];

            //---add each letter to the index array---
            if (![contactIndex containsObject:uniChar])
            {            
                  [contactIndex addObject:uniChar];
            }        
      }

【问题讨论】:

    标签: iphone objective-c ipad nsmutablearray nsarray


    【解决方案1】:

    查看示例,这将起作用

    NSMutableArray *contactsArray = [[NSMutableArray alloc] initWithObjects:@"America",@"Africa",@"america",@"antantica", nil];    
    
    for (int i=0; i<[contactsArray count]; i++){
    
            NSString *alphabet = [[contactsArray objectAtIndex:i] uppercaseString];
    
            NSString *firstCharactor = [alphabet substringToIndex:1];
    
           [contactIndex addObject:firstCharactor];
    
            NSLog(@"%@",firstCharactor);
    
     }
    

    【讨论】:

      【解决方案2】:

      你可以使用老式的 C 函数:toupper(str)

      或者你可以使用uppercaseString()方法:

      NSString* changeString = [upperString uppercaseString];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-22
        • 1970-01-01
        相关资源
        最近更新 更多