1.汉字转拼音,大写转变小写,小写转大写
NSString *tempStr = @"你好,张三"; //转成可变字符串 NSMutableString *tempStr1 = [NSMutableString stringWithString:tempStr]; //先转换为带声调的拼音 CFStringTransform((CFMutableStringRef)tempStr1, NULL, kCFStringTransformToLatin, false); //再转换为不带声调的拼音 CFStringTransform((CFMutableStringRef)tempStr1, NULL, kCFStringTransformStripDiacritics, false); NSLog(@"汉字转拼音:tempStr1 == %@", tempStr1); // ni hao tempStr1 = (NSMutableString *)tempStr1.uppercaseString; NSLog(@"小写转大写:tempStr1 == %@", tempStr1); // NI HAO tempStr1 = (NSMutableString *)tempStr1.lowercaseString; NSLog(@"大写转小写:tempStr1 == %@", tempStr1); // ni hao
2、字典转JSON字符串,JSON字符串转字典
/*! * @brief 把格式化的JSON格式的字符串转换成字典 * @param jsonString JSON格式的字符串 * @return 返回字典 */ + (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString { if (jsonString == nil) { return nil; } NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; NSError *err; NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err]; if(err) { NSLog(@"json解析失败:%@",err); return nil; } return dic; } // 字典转json字符串 + (NSString*)dictionaryToJson:(NSDictionary *)dic { NSError *parseError = nil; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError]; return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; }
3、字符串类型判断
#define ENGLISH @"[a-zA-Z]" // 英文 #define CHINESE @"[u4e00-u9fa5]" // 汉字 #define INTSTR @"[0-9]" // 数字 #define SMALLLETTER @"[a-z]" // 小写字母 #define CAPITALLETTER @"[A-Z]" // 大写字母 /*! * @brief 字符串判断 * @param str 字符串,strRegex 正则表达式 * @return 返回YES或NO */ - (BOOL)typeJudgeWithStr:(NSString *) str withType:(NSString *) strRegex { NSPredicate *strPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", strRegex]; return [strPredicate evaluateWithObject:str]; }