iOS开发NSString+Addition

释放双眼,带上耳机,听听看~!

1.判断是否是空白字符串

+ (BOOL)isBlankString:(NSString *)string;
///判断是否是空白字符串
+ (BOOL)isBlankString:(NSString *)string
{
if (string == nil || string == NULL) {
return YES;
}
if ([string isKindOfClass:[NSNull class]]) {
return YES;
}
if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
return YES;
}
return NO;
}

汉字转拼音

- (NSString *)letters;
///汉字转拼音
- (NSString *)letters
{
NSMutableString *str = [self mutableCopy];
// 先转换为带声调的拼音
if (CFStringTransform ((CFMutableStringRef)str, NULL , kCFStringTransformMandarinLatin , NO )) {
}
// 再转换为不带声调的拼音
if (CFStringTransform (( CFMutableStringRef )str, NULL , kCFStringTransformStripDiacritics , NO )){
}
return str;
}

汉字的第一个字的首字母

- (NSString *)firstCharacter;
///汉字的第一个字的首字母
- ( NSString *)firstCharacter
{
//1. 先传化为拼音
NSString *leters = [self.letters uppercaseString];
//2. 获取首字母
return leters.length?[leters substringToIndex:1]:nil;
}

md5加密

- (NSString *)md5Str;
///md5加密
- (NSString *)md5Str
{
const char *cStr = [self UTF8String];
unsigned char result[16];
CC_MD5(cStr, (CC_LONG)strlen(cStr), result); // This is the md5 call
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]
];
}

sha1加密

- (NSString *)sha1Str;
///sha1加密
- (NSString *)sha1Str
{
const char *cstr = [self cStringUsingEncoding:NSUTF8StringEncoding];
NSData *data = [NSData dataWithBytes:cstr length:self.length];
uint8_t digest[CC_SHA1_DIGEST_LENGTH];
CC_SHA1(data.bytes, (CC_LONG)data.length, digest);
NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
for(int i = 0; i < CC_SHA1_DIGEST_LENGTH; i++)
[output appendFormat:@"%02x", digest[i]];
return output;
}

人已赞赏
iOS文章

iOS蒲公英超级签名原理理

2020-3-5 17:14:03

iOS文章

iOS开发一、NSURLRequest的相关API

2020-3-6 2:08:26

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索