我有一个问题...... 我希望从包含姓名和姓氏的字符串中获取,第一个和姓氏的首字母完整.... 例如:
NSString* myName = @"Mel Gibson";
//I Wish have "M Gibson";
NSString* myName2 = @"Leonardo Di Caprio";
//I wish have "L Di Caprio";
由于
答案 0 :(得分:6)
@implementation NSString (AbbreviateFirstWord)
-(NSString*)stringByAbbreviatingFirstWord {
// step 1: Locate the white space.
NSRange whiteSpaceLoc = [self rangeOfString:@" "];
if (whiteSpaceLoc.location == NSNotFound)
return self;
// step 2: Remove all characters between the first letter and the white space.
NSRange rangeToRemove = NSMakeRange(1, whiteSpaceLoc.location - 1);
return [self stringByReplacingCharactersInRange:rangeToRemove withString:@""];
}
@end