空格前的iphone字符串初始字符

ghi*_*boz 1 iphone split substring nsstring

我有一个问题......我希望从包含姓名和姓氏的字符串中取出,第一个和姓氏的首字母完整....示例:

NSString* myName = @"Mel Gibson";
//I Wish have "M Gibson";

NSString* myName2 = @"Leonardo Di Caprio";
//I wish have "L Di Caprio";
Run Code Online (Sandbox Code Playgroud)

谢谢

ken*_*ytm 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
Run Code Online (Sandbox Code Playgroud)