在iOS中获取字符串中空格后的字符

在iOS中获取字符串中空格后的字符,ios,objective-c,xcode,Ios,Objective C,Xcode,我有这样一个字符串:@“The Happy Day”,我只想将“THD”保存在另一个字符串中。我该怎么做呢?我会这样做 NSString *string = @"The Happy Day"; // Create array of words in string... NSArray *words = [string componentsSeparatedByString:@" "]; // Create array to hold first letter of each word...

我有这样一个字符串:
@“The Happy Day”
,我只想将
“THD”
保存在另一个字符串中。我该怎么做呢?

我会这样做

NSString *string = @"The  Happy Day";

// Create array of words in string...
NSArray *words = [string componentsSeparatedByString:@" "];

// Create array to hold first letter of each word...
NSMutableArray *firstLetters = [NSMutableArray arrayWithCapacity:[words count]];

// Iterate through words and add first letter of each word to firstLetters array...
for (NSString *word in words) {
    if ([word length] == 0) continue;
    [firstLetters addObject:[word substringToIndex:1]];
}

// Join the first letter error into a single string...
NSString *acronym = [firstLetters componentsJoinedByString:@""];

使用Swift中的函数类型(如
map
)可以做得更好,但是没有任何像Objective-C中那样的内置方法。

您是否专门尝试捕获首字母或大写字母?“快乐日”应该返回“TAD”、“ThD”还是“ThD”?对不起,请在我的问题后仔细阅读标题。什么语言?您有一个名为的操作系统和开发环境,但它可以是Objective-C或Swift。嗨,兄弟,我的意思是“快乐的一天”应该返回“ThD”和Objective-C语言。谢谢你,这对我很有帮助me@DongNguyen它应该适用于那个特定的字符串,但我已经修改了我的答案,这样它就跳过了空单词。如果一行中有两个空格,或者字符串两端都有一个空格,那么这种情况可能会发生,并且无论如何都应该解决您遇到的问题。
NSString *str = @"The Happy Day";

NSMutableString *result = [NSMutableString string];
[[str componentsSeparatedByString:@" "] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if(obj){
            [result appendString:[((NSString *)obj) substringToIndex:1]];
        }

 }];