实现中文名取到其首字母的代码
- (NSMutableString *)firstCharactor:(NSString *)aString使用 lowercaseString,uppercaseString,大写字母转化为小写字母
{
NSMutableString *strSum = [[NSMutableString alloc]init];
for (int i = 0 ; i < aString.length ; i++) {
//转成了可变字符串
NSMutableString *str = [NSMutableString stringWithString:[aString substringWithRange:NSMakeRange(i, 1)]];
NSLog(@"---%@",str);
//先转换为带声调的拼音
CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformMandarinLatin,NO);
//再转换为不带声调的拼音
CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformStripDiacritics,NO);
//转化为大写拼音
NSString *pinYin = [str lowercaseString];
NSLog(@"---%@",pinYin);
//获取并返回首字母
strSum = [[NSMutableString alloc]initWithString:[strSum stringByAppendingString:[pinYin substringToIndex:1]]];
}
NSLog(@"拼音%@",strSum);
return strSum;
}
- (void)test{
NSString *testString = @"Hello World";
// lower case
NSString *lowerCaseString1 = [testString lowercaseString];
NSString *lowerCaseString2 = testString.lowercaseString;
// upper case
NSString *upperCaseString1 = [lowerCaseString1 uppercaseString];
NSString *upperCaseString2 = lowerCaseString1.uppercaseString;
NSLog(@"lowerCaseString1: %@",lowerCaseString1);//hello world
NSLog(@"lowerCaseString2: %@",lowerCaseString2);//hello world
NSLog(@"upperCaseString1: %@",upperCaseString1);//HELLO WORLD
NSLog(@"upperCaseString2: %@",upperCaseString2);//HELLO WORLD
// capitalize the first word of the sentence
NSString *txt = @"hi my friends!";
txt = [txt stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[txt substringToIndex:1] uppercaseString]];
NSLog(@"txt: %@", txt);//Hi my friends!
}