1.创建字符串
1 // 1 直接创建字符串 2 NSString * str = @"abc"; 3 4 // NSString * stra = [[NSString alloc] initWithString:@"def"]; 5 // 2 用C语言字符串创建OC字符串 6 char * p = "你好"; 7 NSString * strc = [[NSString alloc] initWithCString:p encoding:NSUTF8StringEncoding];
8 NSLog(@"cstr %@",strc); 9 // 3 格式化的方式创建字符串 10 // format 11 int age = 19; 12 13 NSString * strf = [[NSString alloc] initWithFormat:@"%d岁上的%@",age,@"大二"]; 14 NSLog(@"%@",strf);
2. 字符串的拼接
1 /* 2 //1.已知有两个字符串NSString * str1 = @“hello” NSString * str2 = @“world”,将两个字符串拼接到一起,生成新字符串str,中间用空格分开。 3 4 @"hello world" 5 */ 6 9 // 用printf函数打印 "hello world" 10 printf("%s %s",a,b); 11 NSString * str1 = @"hello"; 12 NSString * str2 = @"world"; 13 14 NSLog(@"%@ %@",str1,str2); 15 16 NSString * str3 = [[NSString alloc] initWithFormat:@"%@ %@",str1,str2]; 17 18 NSLog(@"format: %@",str3); 19
3.获取长度以及单个字符
1 // 1 获取字符串的长度 2 NSString * str = @"qianfeng"; 3 // 获取长度 4 NSUInteger length = [str length]; 5 NSLog(@"%lu",length); 6 7 // 2 获取单个字符 8 unichar c = [str characterAtIndex:7]; 9 // %c 10 NSLog(@"%C",c);
4.获取子串
4.1 index截取前/后 子串
1 // 截取从index开始到结尾的字符串(包括index) 2 NSString * subString1 = [str substringFromIndex:2]; 3 NSLog(@"%@",subString1); 4 // 截取从开始到index的字符串(不包括index) 5 NSString * subString2 = [str substringToIndex:3]; 6 NSLog(@"%@",subString2);
4.2 从X开始截取Y长度的子串
1 // www.baidu.com 2 // 截取 baidu 3 NSString * strB = @"www.baidu.com"; 4 NSRange rangeB = NSMakeRange(4, 5); 5 NSString * subB = [strB substringWithRange:rangeB]; 6 NSLog(@"%@",subB);
4.2 获取子串范围
1 //3.已知字符串NSString * str2 = @“I love Beijing!”; 2 //找出love的范围 3 4 NSString * str = @"I love Beijing!"; 5 NSRange range = [str rangeOfString:@"love"]; 6 if (range.location != NSNotFound) { 7 NSLog(@"%@",NSStringFromRange(range)); 8 } else { 9 NSLog(@"不存在"); 10 }
1 //5.将字符串@"When I was young, I loved a girl in neighbor class."中,从young提取到girl。但是不许数。 2 NSString * str = @"When I was young, I loved a girl in neighbor class."; 3 4 NSRange rangeYoung = [str rangeOfString:@"young"]; 5 NSRange rangeGirl = [str rangeOfString:@"girl"]; 6 // 1234567 6-2 7 NSRange range = {rangeYoung.location,rangeGirl.location-rangeYoung.location+rangeGirl.length}; 8 NSString * newStr = [str substringWithRange:range]; 9 NSLog(@"%@",newStr);
5.字符串的判断
5.1 空字符的判断
nil和@""都是空字符
1 NSString * str1 = @" "; 2 NSString * str = nil; 3 NSString * str2 = @""; 4 if (str1 == nil || [str1 length] == 0) { 5 NSLog(@"空字符串"); 6 } else { 7 NSLog(@"非空"); 8 }
5.2 判断是否以指定内容开头/结尾
1 // 2 判断字符串是否以指定的内容开头 2 NSString * str3 = @"www.qianfeng.com"; 3 BOOL hasPre = [str3 hasPrefix:@"www"]; 4 // YES NO 5 NSLog(@"%d",hasPre); 6 if (hasPre) { 7 NSLog(@"是一个网址"); 8 } else { 9 NSLog(@"不是一个网址"); 10 } 11 12 // qianfeng.txt 13 NSString * str4 = @"qianfeng.txt"; 14 BOOL hasSuf = [str4 hasSuffix:@"txt"]; 15 if (hasSuf) { 16 NSLog(@"是txt"); 17 } else { 18 NSLog(@"不是txt"); 19 }
5.3判断字符串是否相等
1 NSString * str5 = @"qianfeng"; 2 NSString * str6 = [[NSString alloc] initWithFormat:@"qianfeng"]; 3 NSString * str7 = @"qianfeng"; 4 NSString * str8 = [[NSString alloc] initWithFormat:@"qianfeng"]; 5 NSLog(@"%p %p %p",str5,str6,str7); 6 // 判断地址是否相等 7 if (str5 == str6) { 8 NSLog(@"相等"); 9 } else { 10 NSLog(@"不相等"); 11 } 12 // 判断内容是否相等 13 if ([str5 isEqualToString:str6]) { 14 NSLog(@"相等"); 15 } else { 16 NSLog(@"不相等"); 17 } 18
5.4 字符串大小比较
compare方法
1 NSString * str9 = @"1235"; 2 NSString * str10 = @"124"; 3 // compare 4 NSComparisonResult result = [str9 compare:str10]; 5 switch (result) { 6 case NSOrderedAscending: 7 NSLog(@"左边<右边"); 8 break; 9 case NSOrderedSame: 10 NSLog(@"左边=右边"); 11 break; 12 case NSOrderedDescending: 13 NSLog(@"左边>右边"); 14 break; 15 default: 16 break;
时间: 2024-12-07 19:34:55