Foundation之NSString
一、NSString的创建
1 int main(int argc, const char * argv[]) 2 { 3 4 @autoreleasepool { 5 6 //这种方式不需要release 7 NSString *s1 = @"this is a string"; 8 NSLog(@"s1=%@", s1); 9 10 //这种方式需要release 11 NSString *s2 = [[NSString alloc] init]; 12 s2 = @"this is a string"; 13 NSLog(@"s2=%@", s2); 14 [s2 release]; 15 16 //这种方式需要release 17 NSString *s3 = [[NSString alloc] initWithString:@"this is a string"]; 18 NSLog(@"s3=%@", s3); 19 [s3 release]; 20 //这种方式不需要release 21 s3 = [NSString stringWithString:@"this is a string"]; 22 NSLog(@"s3=%@", s3); 23 24 //这种方式需要release 25 NSString *s4 = [[NSString alloc] initWithUTF8String:"this is a string"]; 26 NSLog(@"s4=%@", s4); 27 [s4 release]; 28 //这种方式不需要release 29 s4 = [NSString stringWithUTF8String:"this is a string"]; 30 NSLog(@"s4=%@", s4); 31 32 //这种方式需要release 33 NSString *s5 = [[NSString alloc] initWithFormat:@"my age is %i and height is %.2f", 18, 1.7f]; 34 NSLog(@"s5=%@", s5); 35 [s5 release]; 36 //这种方式不需要release 37 s5 = [NSString stringWithFormat:@"my age is %i and height is %.2f", 18, 1.7f]; 38 NSLog(@"s5=%@", s5); 39 40 } 41 return 0; 42 }
二、NSString的常用方法
1 int main(int argc, const char * argv[]) 2 { 3 4 @autoreleasepool { 5 6 NSString *s1 = @"This is a String"; 7 // 转大写 8 NSLog(@"%@", [s1 uppercaseString]); //THIS IS A STRING 9 // 转小写 10 NSLog(@"%@", [s1 lowercaseString]); //this is a string 11 // 每个单词的首字母转大写 12 NSLog(@"%@", [s1 capitalizedString]); //This Is A String 13 14 // 比较两个字符串的内容是否相同 15 BOOL r1 = [@"abc" isEqualToString:@"Abc"]; 16 NSLog(@"%i", r1); //0 17 18 // 逐个字符进行比较大小 19 NSComparisonResult r2 = [@"abc" compare:@"Abc"]; 20 if (r2==NSOrderedSame) { 21 NSLog(@"两个字符串的内容相同"); 22 } else if (r2==NSOrderedAscending) { 23 NSLog(@"右边 > 左边"); 24 } else if (r2==NSOrderedDescending) { 25 NSLog(@"右边 < 左边"); 26 } 27 28 // 是否以This开头 29 BOOL r3 = [s1 hasPrefix:@"This"]; 30 NSLog(@"%i", r3); //1 31 // 是否以String结尾 32 BOOL r4 = [s1 hasSuffix:@"String"]; 33 NSLog(@"%i", r4); //1 34 35 // 查找is在s1中的位置, 36 NSRange r5 = [s1 rangeOfString:@"is"]; 37 if (r5.location == NSNotFound) { 38 NSLog(@"找不到"); 39 } else { 40 NSLog(@"%@", NSStringFromRange(r5)); //{2, 2} 41 } 42 43 // 从尾部查找is在s1中的位置, 44 NSRange r6 = [s1 rangeOfString:@"is" options:NSBackwardsSearch]; 45 if (r6.location == NSNotFound) { 46 NSLog(@"找不到"); 47 } else { 48 NSLog(@"%@", NSStringFromRange(r6)); //{5, 2} 49 } 50 51 // 从索引3开始截取到末尾 52 NSLog(@"%@", [@"123456" substringFromIndex:3]); //456 53 // 从头部截取到索引3之前 54 NSLog(@"%@", [@"123456" substringToIndex:3]); //123 55 // 指定范围截取 56 NSLog(@"%@", [@"123456" substringWithRange:NSMakeRange(2, 3)]); //345 57 58 // 分隔字符串 59 NSArray *arr = [@"1,2,3,4,5" componentsSeparatedByString:@","]; 60 NSLog(@"%@", arr[2]); //3 61 62 // 字符串长度 63 NSLog(@"%zi", [@"abc" length]); //3 64 // 转为int 65 NSLog(@"%i", [@"12" intValue]); 66 67 } 68 return 0; 69 }
三、从文件中读取文本
1 int main(int argc, const char * argv[]) 2 { 3 4 @autoreleasepool { 5 6 NSString *path = @"/Users/Lane/Desktop/test.txt"; 7 8 NSError *error; 9 10 NSString *s1 = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error]; 11 12 if (error) { 13 NSLog(@"文件读取失败: %@", error); 14 } else { 15 NSLog(@"%@", s1); 16 } 17 18 19 } 20 return 0; 21 }
四、从URL中读取文本
1 int main(int argc, const char * argv[]) 2 { 3 4 @autoreleasepool { 5 6 NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"]; 7 8 NSError *error; 9 10 NSString *s1 = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error]; 11 12 if (error) { 13 NSLog(@"URL读取失败: %@", error); 14 } else { 15 NSLog(@"%@", s1); 16 } 17 18 19 } 20 return 0; 21 }
五、写入文本文件
1 int main(int argc, const char * argv[]) 2 { 3 4 @autoreleasepool { 5 6 NSString *path = @"/Users/Lane/Desktop/test2.txt"; 7 8 NSError *error; 9 10 NSString *s1 = @"123456"; 11 [s1 writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error]; 12 13 if (error) { 14 NSLog(@"文件写入失败: %@", error); 15 } else { 16 NSLog(@"文件写入成功"); 17 } 18 19 20 } 21 return 0; 22 }