//NSString 转换为char *
NSString *blankText = @"sevensoft is a mobile software outsourcing company";
char *ptr = [blankText cStringUsingEncoding:NSASCIIStringEncoding];
//char * 转换为 NSString
char encode_buf[1024];
NSString *encrypted = [[NSString alloc] initWithCString:(const char*)encode_buf encoding:NSASCIIStringEncoding];
char mychar[100];
mystring=[NSString stringWithFormat:@"%s",mychar];
char[]转NSString有更简单的方法,NSString类方法:
+ (id)stringWithUTF8String:(const char *)bytes。
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
NSString *str = [[NSString alloc] initWithCString:(char*) encoding:enc]; //使用NSUTF8StringEncoding编码无效时,改为如上编码进行尝试(中文能正常显示)
去两边空格
- NSString *cleanString = [dirtyString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];