iOS 获取字符串的首字母, 缺省为#
2020-11-21 本文已影响0人
gaookey
- (NSString*)firstChar:(NSString *)string {
if ((string == nil)|| (string == NULL) || ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0) || [string isKindOfClass:[NSNull class]]) {
return @"#";
}
NSMutableString *source = [NSMutableString stringWithString:string];
CFStringTransform((CFMutableStringRef)source, NULL, kCFStringTransformMandarinLatin, NO);
CFStringTransform((CFMutableStringRef)source, NULL, kCFStringTransformStripDiacritics, NO);
return [[source capitalizedString] substringToIndex:1];
}