标签:
代码:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//第一种方法
NSLog(@"--first-%i",[self convertToInt:@"123我爱你"]);
//第二种方法
NSLog(@"--second--%ld",[self getToInt:@"123我爱你"]);
}
#pragma -mark -functions
//得到中英文混合字符串长度 方法1
- (int)convertToInt:(NSString*)strtemp
{
int strlength = 0;
char* p = (char*)[strtemp cStringUsingEncoding:NSUnicodeStringEncoding];
for (int i=0 ; i<[strtemp lengthOfBytesUsingEncoding:NSUnicodeStringEncoding] ;i++) {
if (*p) {
p++;
strlength++;
}
else {
p++;
}
}
return strlength;
}
//得到中英文混合字符串长度 方法2
- (long)getToInt:(NSString*)strtemp
{
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
NSData* da = [strtemp dataUsingEncoding:enc];
return [da length];
}
标签:
原文地址:http://www.cnblogs.com/yang-guang-girl/p/4607768.html