1、中文转拼音
+ (NSString *)firstCharactor:(NSString *)aString
{
if (aString.isNoEmpty) {
//转成了可变字符串
NSMutableString *str = [NSMutableString stringWithString:aString];
//先转换为带声调的拼音
CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformMandarinLatin,NO);
//再转换为不带声调的拼音
CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformStripDiacritics,NO);
//转化为大写拼音
// NSString *pinYin = [str capitalizedString];
//获取并返回首字母
// return [pinYin substringToIndex:1];
return str;
}
return aString;
}
//判断是否为空,处理服务器返回多种多样空类型的情况
- (BOOL)isNoEmpty
{
if ([self isKindOfClass:[NSNull class]])
{
return NO;
}
else if ([self isKindOfClass:[NSString class]])
{
NSString *str = (NSString *)self;
return [str length] > 0;
}
else if ([self isKindOfClass:[NSData class]])
{
return [(NSData *)self length] > 0;
}
else if ([self isKindOfClass:[NSArray class]])
{
return [(NSArray *)self count] > 0;
}
else if ([self isKindOfClass:[NSDictionary class]])
{
return [(NSDictionary *)self count] > 0;
}
return YES;
}
2、MD5
- (NSString*)md5
{
const char *cStr=[self UTF8String];
unsigned char result[CC_MD5_DIGEST_LENGTH];
CC_MD5(cStr,(CC_LONG)strlen(cStr), result);
return [NSString stringWithFormat:
@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
result[0], result[1], result[2], result[3],
result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11],
result[12], result[13], result[14], result[15]
];
}
3、去掉字符串中的空格
- (NSString *)trimString
{
// 截断字符串中的所有空白字符(空格,\t,\n,\r)
return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
网友评论