NSString使用

来源:互联网 发布:维奈斯淘客采集软件 编辑:程序博客网 时间:2024/05/17 01:32

NSString继承自NSObject对象,在OC中作为字符串类型,就像Java中的string一样,值得注意的是系统自带的对象都有一个和对象方法相同的类方法完成相同的功能,也就是说有一个静态方法和一个动态方法,而且静态方法不用我们管理内存,所以开发中都用静态方法。


基本使用和文件读取

void createString(){

    NSString *str=@"this is string";//自动释放

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

    

    NSString *s2=[[NSStringalloc]initWithString:@"this isstring"];

    NSString *s3=[[NSStringalloc]initWithFormat:@"name=%s and no=%i","cooljune",12];

    

    NSString *s4= [NSStringstringWithString:@"5454"];//等价于NSString *str=@"this is string";

    NSString *s5=[NSStringstringWithFormat:@"45454"];

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

    //文件读取字符串

    NSString *path=@"/Users/user/desktop/testxt.txt";

   NSError *error=nil;//先清空

    NSString *filecont= [[NSStringalloc]initWithContentsOfFile:pathencoding:NSUTF8StringEncodingerror:&error];

    //NSLog(@"fileconten=%@,错误信息%@",filecont,error);

   if (error) {

        NSLog(@"failed,messgae=%@",[errorlocalizedDescription]);

    }else{

        NSLog(@"success,content=%@",filecont);

    }

    NSURL *url=[NSURLURLWithString:@"http://i2.didaimg.com//api/openapi?city=beijing"];

    NSURL *fileUrl=[NSURLURLWithString:@"file:///Users/user/desktop/testxt.txt"];

    NSURL *furl=[NSURLfileURLWithPath:@"/Users/user/desktop/testxt.txt"];

    NSString *s6=[[NSStringalloc]initWithContentsOfURL:furlencoding:NSUTF8StringEncodingerror:nil];

    //[NSString stringWithContentsOfURL:<#(NSURL *)#> encoding:<#(NSStringEncoding)#> error:<#(NSError **)#>];

    NSLog(@"url content=%@",s6);

    [s2release];

    [s3release];

    [filecontrelease];

    [s6release];

}


字符串搜索和截取:

void stringUseSearch(){

    NSString *s=@"http://weixin.com?wxNo=cooljune&pwd=upsbus";

   NSRange range=[srangeOfString:@"wxNo"];//范围长度

    NSLog(@"range=%@",NSStringFromRange(range));

    

    //如果找不到

   NSRange ran=[srangeOfString:@"https://"];

   if(ran.length==0){

       NSLog(@"not find");

    }else{

        NSLog(@"range=%@",NSStringFromRange(ran));

    }

    //是否以什么开头

   BOOL flag=[shasPrefix:@"http"];

    //判断结尾(文件后缀名)

    BOOL flag2=[@"test.excel"hasSuffix:@".excel"];

   NSLog(@"flag=%i",flag2);

}

void stringUseSub(){

    NSString *s=@"http://weixin.com?wxNo=cooljune&pwd=upsbus";

   NSRange norange=[srangeOfString:@"wxNo="];

   NSUInteger location=norange.location+norange.length;

   NSUInteger length=[srangeOfString:@"&"].location-location;

   NSString *no=[ssubstringWithRange:NSMakeRange(location, length)];

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

   // nsstring *pwd=@"";

   NSRange prange=[srangeOfString:@"pwd="];

   NSUInteger plocation=prange.location+prange.length;

   NSUInteger plength=s.length-plocation;

   // NSString *pwd=[s substringWithRange:NSMakeRange(plocation, plength)];

    //从某个位置开始一直截取到结尾

   NSString *sp=[ssubstringFromIndex:plocation];

    //从首位置开始截取到某个位置

    NSString *sp2=[ssubstringToIndex:5];

   NSLog(@"pwd==%@",sp);

}

大小写转换:

/*大小写转换*/

void uperAndlower(){

   NSString *str=@"abcAbc";

    NSLog(@"str=%@",[struppercaseString]);

    NSLog(@"str=%@",[strlowercaseString]);

}

字符串比较:

/*比较*/

void compare(){

    NSString *s1=@"coolJune";

   NSString *s2=@"csdn";

    

   BOOL b=[s1isEqualToString:s2];

    NSLog(@"bool=%i",b);

       

  NSComparisonResult result= [s1compare:s2];

    

   if(result==NSOrderedSame){

       NSLog(@"内容相同");

    }

    elseif (result==NSOrderedAscending){

       NSLog(@"asc");

    }else{

        NSLog(@"NSOrderedDescending-->desc");

    }

}

写入文件

/*写入文件*/

//如果文件夹不存在,写入失败

//如果文件不存在会创建文件

//atomically原子性保证数据完整性先创建一个临时文件,如果临时文件创建成功,才会创建,如果中间失败,那么临时文件将被删除

void stringimport(){

   NSString *str=@"tefdgst";

    NSString *path=@"/Users/user/desktop/testvxt.txt";

   NSError *error=nil;

     BOOL flag=[strwriteToFile:pathatomically:YESencoding:NSUTF8StringEncodingerror:&error];

   if(error){

        NSLog(@"srrory%@",[errorlocalizedDescription]);

    }else{

       NSLog(@"success");

    }

}

这里说明一下关于原子性,其实就像事务一样,要么执行成功,要么都执行失败,使用原子性可以保证数据完整性。


字符串内存泄露案例:

void errorStringCase(){

    //字符串内存泄露,因为没有释放

    NSString *str=[[NSStringalloc]initWithString:@"test"];

    //字符串是自动释放,不需要手动释放,所以release会过度释放

    str=@"45454";   

    [strrelease];

}



所以当程序运行到[str release]的时候就过度释放,因为字符串"45454"对象并不需要我们管理内存,而test对象我们一直没有释放,所以造成内存泄露。


        NSString *str=@"7456";

       NSLog(@"----%i",[strintValue]);

        //oc字符串转c语言字符串

       constchar *s=[str UTF8String];

      //c语言的字符串转为Oc的字符串

      char *ss="sss";

      [[NSString alloc]initWithUTF8String:ss];




0 0