/** 获取字符串 字节数 @return 返回字节数 备注:1.getCharasLength方法写在NSString 分类中 2.NSUInteger charsNum = [regexStr getCharasLength]; */ - (NSUInteger)getCharasLength { NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000); return [self getCharasLengthWithEncoding:encoding]; } - (NSUInteger)getCharasLengthWithEncoding:(NSStringEncoding)encoding { NSUInteger strLength = 0; char *q = (char *)[self cStringUsingEncoding:encoding]; NSUInteger lengthBytes = [self lengthOfBytesUsingEncoding:encoding]; for (int i = 0; i < lengthBytes; i++) { if (*q) { q++; strLength++; } else { q++; } } return strLength; }