Skip to content

Instantly share code, notes, and snippets.

@dabing1022
Created September 16, 2015 03:18
Show Gist options
  • Save dabing1022/cb2903394378f1720c11 to your computer and use it in GitHub Desktop.
Save dabing1022/cb2903394378f1720c11 to your computer and use it in GitHub Desktop.
获得字符串的字符数
// @"一中华abc" 8
- (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;
}
// @"一中华abc" 9
- (int)getToInt:(NSString*)strtemp
{
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
NSData* da = [strtemp dataUsingEncoding:enc];
return[da length];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment