使用ASIHttpRequest进行网络请求的时候,如果包含的参数中含有中文,可调用如下方法对汉字进行处理
- (NSString*)URLEncode:(NSString *)originalString{ NSArray *escapeChars = [NSArray arrayWithObjects:@";" , @"/" , @"?" , @":" , @"@" , @"&" , @"=" , @"+" , @"$" , @"," , @"!", @"'", @"(", @")", @"*", nil]; NSArray *replaceChars = [NSArray arrayWithObjects:@"%3B" , @"%2F", @"%3F" , @"%3A" , @"%40" , @"%26" , @"%3D" , @"%2B" , @"%24" , @"%2C" , @"%21", @"%27", @"%28", @"%29", @"%2A", nil]; int len = [escapeChars count]; NSMutableString *temp = [[originalString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] mutableCopy]; int i; for (i = 0; i < len; i++) { [temp replaceOccurrencesOfString:[escapeChars objectAtIndex:i] withString:[replaceChars objectAtIndex:i] options:NSLiteralSearch range:NSMakeRange(0, [temp length])]; } NSString *outStr = [NSString stringWithString: temp]; return outStr; }
计算含有中英文的字符串的长度(如输入:你好,字符串长度应为4):
- (int)getToInt:(NSString*)strtemp { NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000); NSData* da = [strtemp dataUsingEncoding:enc]; NSLog(@"------%d",[da length]); return [da length]; }