(int)convertToInt:(NSString*)strtemp {
int strlength = 0;
// 这里一定要使用gbk的编码方式,网上有很多用Unicode的,但是混合的时候都不行
NSStringEncoding gbkEncoding =CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
char* p = (char*)[strtemp cStringUsingEncoding:gbkEncoding];
for (int i=0 ; i<[strtemp lengthOfBytesUsingEncoding:gbkEncoding] ;i++) {
if (p) {
p++;
strlength++;
}
else {
p++;
}
}
return strlength;
int strlength = 0;
// 这里一定要使用gbk的编码方式,网上有很多用Unicode的,但是混合的时候都不行
NSStringEncoding gbkEncoding =CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
char* p = (char*)[strtemp cStringUsingEncoding:gbkEncoding];
for (int i=0 ; i<[strtemp lengthOfBytesUsingEncoding:gbkEncoding] ;i++) {
if (p) {
p++;
strlength++;
}
else {
p++;
}
}
return strlength;
}
用utf8编码一样可以算啊
+(int)calculateTextNumber:(NSString *) textA
{
float number = 0.0;
int index;
for (index=0; index < [textA length]; index++) {
NSString *character = [textA substringWithRange:NSMakeRange(index, 1)];
if ([character lengthOfBytesUsingEncoding:NSUTF8StringEncoding] == 3) {
number++;
} else {
number = number+0.5;
}
}
return ceil(number);
}