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)
谢谢
@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)
| 归档时间: |
|
| 查看次数: |
349 次 |
| 最近记录: |