iOS开发 字符串的转化 小技巧
Posted 回之声
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS开发 字符串的转化 小技巧相关的知识,希望对你有一定的参考价值。
/字典或者数组转化为nsstring
NSArray *arr = [NSArray arrayWithObject:@"1"];
NSString *str = [arr JSONString]; //数组或者字典转化为json串
NSArray *arr1 = [str objectFromJSONString]; //json串转化为字典或者数组
NSData *data = [arr1 JSONData]; //把json串转化为nsdata
NSString *str1 = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];//把NSdata转为为NSString
NSData *data1 = [str1 dataUsingEncoding:NSUTF8StringEncoding]; //把NSString转化为NSData
//二进制流转变为字符串
NSString *str=[[NSMutableString alloc] initWithData:self.receiveDataencoding:NSUTF8StringEncoding];
//转变其中的内容
str=[str stringByReplacingOccurrencesOfString:@"," withString:@",\n"];
str=[str stringByReplacingOccurrencesOfString:@"{" withString:@"{\n"];
str=[str stringByReplacingOccurrencesOfString:@"}" withString:@"\n}"];
NSLog(@"%@",str);
//把字符串再转变为二进制流
self.receiveData=[NSMutableData dataWithData:[str dataUsingEncoding:NSUTF8StringEncoding]];
//request转变为URL
NSURL *url=[request URL];
//URL转变为nsstring
NSString *str=[[url absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@",str);
NSURL *url=[NSURL URLWithString:@"http://img3.xooimage.com/files/6/1/3/firefox-logo-200x200-19f78a.png"];
//网站转变为二进制流
NSMutableData *data=[NSMutableData dataWithContentsOfURL:url];
UIImage *image=[UIImage imageWithData:data];
//读取文件
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Students" ofType:@"xml"];
//文件转变为二进制流
NSData *data = [NSData dataWithContentsOfFile:filePath];
//取出首字母
NSString *name = [city objectForKey:@"name"];
NSString *firstStr=[name substringWithRange:NSMakeRange(0, 1)];
//从一个字符串中直接取出数组
NSString *str = [NSString stringWithFormat:@"Norman,Stanley,Fletcher"];
NSArray *arr = [str componentsSeparatedByString:@","];
NSLog(@"arr = %@",arr);
//转码
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
以上是关于iOS开发 字符串的转化 小技巧的主要内容,如果未能解决你的问题,请参考以下文章
iOS开发小技巧--修改按钮内部图片和文字之间的间距(xib)