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];

posted @ 2016-03-24 16:56  挽梦忆笙音  阅读(357)  评论(0编辑  收藏  举报