iOS 字符转换

1.字典或者数组转化为NSString

NSArray*arr= [NSArrayarrayWithObject:@"1"];

NSString*str= [arrJSONString];//数组或者字典转化为json串

NSArray*arr1= [strobjectFromJSONString];//json串转化为字典或者数组

NSData*data= [arr1JSONData];//把json串转化为Nsdata

NSString*str1= [[NSStringalloc]initWithData:dataencoding:NSUTF8StringEncoding];

//把NSdata转为为NSString
NSData*data1= [str1dataUsingEncoding:NSUTF8StringEncoding];//把NSString转化为NSData



2.二进制流转变为字符串

NSString*str=[[NSMutableStringalloc]initWithData:self.receiveDataencoding:NSUTF8StringEncoding];

//转变其中的内容

str=[strstringByReplacingOccurrencesOfString:@","withString:@",\n"];

str=[strstringByReplacingOccurrencesOfString:@"{"withString:@"{\n"];

str=[strstringByReplacingOccurrencesOfString:@"}"withString:@"\n}"];

NSLog(@"%@",str);

//把字符串再转变为二进制流

self.receiveData=[NSMutableDatadataWithData:[strdataUsingEncoding:NSUTF8StringEncoding]];

3.request转变为URL

NSURL*url=[requestURL];

//URL转变为nsstring

NSString*str=[[urlabsoluteString]stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSLog(@"%@",str);

NSURL*url=[NSURLURLWithString:@"http://img3.xooimage.com/files/6/1/3/firefox-logo-200x200-19f78a.png"];

4.网站转变为二进制流

NSMutableData*data=[NSMutableDatadataWithContentsOfURL:url];

UIImage*image=[UIImageimageWithData:data];

4.读取文件

NSString*filePath

= [[NSBundlemainBundle]pathForResource:@"Students"ofType:@"xml"];

5.文件转变为二进制流

NSData*data

= [NSDatadataWithContentsOfFile:filePath];

//取出首字母

NSString*name

= [cityobjectForKey:@"name"];

NSString*firstStr=[namesubstringWithRange:NSMakeRange(0,1)];

6.从一个字符串中直接取出数组

NSString*str

= [NSStringstringWithFormat:@"Norman,Stanley,Fletcher"];

NSArray*arr

=

[strcomponentsSeparatedByString:@","];

NSLog(@"arr= %@",arr);

6.转码

urlStr=[urlStrstringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

你可能感兴趣的:(iOS 字符转换)