几种常见的用法为:字符串的创建、字符串的搜索、字符串的比较、字符串的转换
用途一:字符串的创建
1 void ex1() 2 { 3 //1.常量字符串的对象 4 NSString *str1 = @"hello world"; 5 NSLog(@"str1 = %@",str1); 6 //2.通过类方法创建字符串 7 NSString *str2 = [NSString stringWithString:str1]; 8 NSLog(@"str2 = %@",str2); 9 //3.通过实例方法创建 10 NSString *str3 = [[NSString alloc]initWithString:str1]; 11 NSLog(@"str3 = %@",str3); 12 //4.格式化字符串方式创建 13 NSString *str4 = [NSString stringWithFormat:@"name=%@,password=%@",@"admin",@"123456"]; 14 NSLog(@"str4 = %@",str4); 15 //5.通过C语言字符串的创建 16 char *pstr = "how are you?"; 17 NSString *str5 = [NSString stringWithUTF8String:pstr]; 18 NSLog(@"str5 = %@",str5); 19 //6.通过文件的方式创建字符串 20 NSError *error = nil; 21 NSUInteger enc; 22 NSString *str6 = [[NSString alloc]initWithContentsOfFile:@"/Users/mac/Java/IO/2.txt" usedEncoding:&enc error: &error]; 23 if(!error) 24 { 25 NSLog(@"str6 = %@",str6); 26 } 27 else 28 { 29 NSLog(@"读取文件失败:%@",error); 30 } 31 //7.通过网络数据创建字符串 32 NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"]; 33 NSString *str7 = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error]; 34 if(!error) 35 { 36 NSLog(@"str7 = %@",str7); 37 } 38 else 39 { 40 NSLog(@"读取网络失败:%@",error); 41 } 42 }
用途二:字符串的搜索
1 void ex2() 2 { 3 //1.搜索字符串 4 NSString *str = @"this is a test"; 5 NSRange nsr = [str rangeOfString:@"test"];//返回一个结构体 6 if(nsr.length != 0)//找到字符串 7 { 8 NSLog(@"location=%lu,length=%lu",nsr.location,nsr.length); 9 } 10 //2.查找字符串是不是以什么前缀开始 11 NSString *str2 = @"http://www.sina.com.cn"; 12 if([str2 hasPrefix:@"http"]) 13 { 14 NSLog(@"http协议"); 15 } 16 //3.查找字符串是不是以什么后缀开始 17 NSString *str3 = @"1.txt"; 18 if([str3 hasSuffix:@"txt"]) 19 { 20 NSLog(@"文本文件"); 21 } 22 //4.在字符串中截取字串 23 NSString *str4 = @"http://www.sxt.cn?user=admin&password=1233456"; 24 NSRange range1 = [str4 rangeOfString:@"user="]; 25 NSRange range2 = [str4 rangeOfString:@"&password="]; 26 27 //求出user的字符串区间并输出字符串 28 //用户名起始位置 29 NSUInteger location = range1.location + range1.length; 30 //用户名的有效长度 31 NSUInteger length = range2.location - location; 32 33 NSRange range3 = NSMakeRange(location,length); 34 NSString *user = [str4 substringWithRange:range3]; 35 36 //求出password的字符串 37 NSString *password = [str4 substringFromIndex:range2.location+range2.length]; 38 39 NSLog(@"user=%@,password=%@",user,password); 40 }
用途三:字符串的比较
1 void ex3() 2 { 3 //对象比较 4 //1.判断两个对象是不是同一个对象 5 NSString *str1 = @"hello world"; 6 NSString *str2 = @"how are you"; 7 NSString *str3 = str1; 8 if(str1 == str3) 9 { 10 NSLog(@"str1和str3是同一个对象"); 11 } 12 //或者 13 if([str1 isEqual:str3]) 14 { 15 NSLog(@"str1和str3是同一个对象"); 16 } 17 //或者 18 if([str1 isEqualTo:str3]) 19 { 20 NSLog(@"str1和str3是同一个对象"); 21 } 22 23 //2.判断字符串是否相等 24 if([str1 isEqualToString:@"hello world"]) 25 { 26 NSLog(@"字符串相等"); 27 } 28 29 //3.字符串的比较 30 NSComparisonResult result = [str1 compare:str2]; 31 if(result== NSOrderedSame) 32 { 33 NSLog(@"两个字符串相等"); 34 } 35 else if(result == NSOrderedAscending) 36 { 37 NSLog(@"第一个小于第二个字符串"); 38 } 39 else 40 { 41 NSLog(@"第一个大于第二个字符串"); 42 } 43 }
用途四:字符串的转换
1 void ex4() 2 { 3 //1.其它类型转换成字符串 4 NSString *str1 = [NSString stringWithFormat:@"%D,%.2f,%c,%s",10,12.34,'A',"hello world"]; 5 NSLog(@"str1 = %@",str1); 6 7 //2.字符串转换为整型 8 NSString *str2 = @"20"; 9 NSInteger i = [str2 integerValue]; 10 NSLog(@"i = %ld",i); 11 12 //3.字符串转换为实型 13 NSString *str3 = @"12.34"; 14 CGFloat f = [str3 doubleValue]; 15 NSLog(@"f = %.2f",f); 16 17 //4.字符串转换为C语言的字符串 18 NSString *str4 = @"how are you"; 19 const char *pstr = [str4 UTF8String]; 20 NSLog(@"pstr = %s",pstr); 21 22 }