这个问题得回归倒基础上面。字符串的长度跟编码有关系。utf8是变长,gbk是双字节
所以看如下代码
NSString *test = [NSString stringWithString:@"这是一个中文test1"];
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
NSLog(@”%@ length is: %d”,test,[test lengthOfBytesUsingEncoding:enc]);
计算NSString字节长度,中文如何按2字节计算
Posted in Life.
– July 28, 2011
0 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.
You must be logged in to post a comment.