iOS 根据字节切割字符串

//获取字符串的字节数
- (NSUInteger )getToInt:(NSString*)strtemp
{
    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
    NSData* da = [strtemp dataUsingEncoding:enc];
    return [da length];
}
//切割字符串
-(NSString *)subStringWithString:(NSString *)string withLength:(NSInteger )count
{
   NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
   NSData* data = [string dataUsingEncoding:enc];
   NSData * subData;
   if (data.length%2 == 0) {
       subData = [data subdataWithRange:NSMakeRange(0, count)];
   }else{
        subData = [data subdataWithRange:NSMakeRange(0, count - 1)];
   }
    return [[NSString alloc] initWithData:subData encoding:enc];
}

你可能感兴趣的:(iOS 根据字节切割字符串)