你的位置:首页 > 操作系统

[操作系统]NSString 的常用操作


 

    NSString *testStr01=@"HelloWord";    NSString *testStr02=[testStr01 substringToIndex:5];//取头(从头到哪),to不包括下标内容    NSString *testStr03=[testStr01 substringFromIndex:5];//去尾(从哪到尾),from包括下标内容    NSLog(@"testStr02:%@ testStr03:%@",testStr02,testStr03);        NSRange r={4,3};//取中间,从哪长度是多少    NSString*testStr04=[testStr01 substringWithRange:r];    NSLog(@"testStr04:%@",testStr04);                //字符串的拼接    NSString *testStr05 = @"Hello";    //追加    NSString *testStr06 = [testStr05 stringByAppendingString:@"World"];    NSLog(@"testStr06:%@",testStr06);    //初始化    NSString *testStr07 = @"Hello";    NSString *testStr08 = @"World";    NSString *testStr09 = [NSString stringWithFormat:@"%@%@",testStr07,testStr08];    NSString *testStr10 = [NSString stringWithFormat:@"Hello%@",testStr08];    NSLog(@"testStr09:%@",testStr09);    NSLog(@"testStr10:%@",testStr10);            //按指定格式(范围)追加内容    NSString *testStr11 = @"Hello";    NSString *testStr12 = [testStr11 stringByAppendingFormat:@"%@%@",@"World",@"123"];    NSLog(@"testStr12:%@",testStr12);            //替换    NSString *testStr13 = @"www.kyle.com.cn";    NSString *testStr14 = [testStr13 stringByReplacingCharactersInRange:NSMakeRange(4, 6) withString:@"163"];    NSLog(@"testStr14:%@",testStr14);            //编码集    //参数 文件的路径 不包括文件名    NSString *path = @"/Users/apple/Desktop";    //path = [path stringByAppendingString:@"/test.txt"];    path = [path stringByAppendingString:@"/test.rtf"];    //把文件中的内容 读取到字符串中    //NSString *str20 = [[NSString alloc]initWithContentsOfFile:path];        NSString *testStr15 = [[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];        NSLog(@"testStr15:%@",testStr15);            NSString* use=@"fcp";    NSString* use1=@"fcp";        NSString*password=@"123";    NSString*password1=@"123";    if ([use isEqualToString:use1]&&[password isEqualToString:password1]) {      NSLog(@"登陆成功");    }    else{      NSLog(@"登陆失败");    }