标签:
1.NSString的创建方法
//1.快速创建 NSString *str1 = @"白日依山尽"; //2.用alloc创建 NSString *str2 = [[NSString alloc]initWithString:@"黄河入海流"]; //3.用格式化符创建字符串 NSString *str3 = [[NSString alloc]initWithFormat:@"%@",@"欲穷千里目"]; //4.类方法格式化符创建 NSString *str4 = [NSString stringWithFormat:@"%@",@"更上一层楼"];
2.比较字符串
//比较字符串是否相同 方法一 比较的是字符串中的内容 BOOL count1 = [str1 isEqualToString:str2]; NSLog(@"count1 = %d",count1); //比较字符串是否相同 方法二 比较的是字符串的地址 BOOL count2 = (str2 == str3); NSLog(@"count2 = %d",count2); //比较字符串大小 NSComparisonResult result = [str3 caseInsensitiveCompare:str4]; if(result == NSOrderedAscending) NSLog(@"升序"); else if(result == NSOrderedSame) NSLog(@"相等"); else if (result == NSOrderedDescending) NSLog(@"降序");
3.获取字符串的长度
//获取到字符串长度 NSInteger strLength = [str4 length]; NSLog(@"strLength = %ld",strLength);
4.字符串的转换
//字符串的转换 NSString *str5 = @"Hello World"; //全部转换成小写 NSLog(@"str5 = %@",[str5 lowercaseString]); //全部转换成大写 NSLog(@"str5 = %@",[str5 uppercaseString]); //每个单词的首字母大写,其余小写 NSLog(@"str5 = %@",[str5 capitalizedString]);
5.字符串转换成基本数据类型
//转换成整型 NSString *str6 = @"3456789"; NSLog(@"str6 = %ld",[str6 integerValue]); //转换成浮点型 NSString *str7 = @"12321.34832"; NSLog(@"str7 = %lf",[str7 floatValue]); //转换成布尔类型 NSString *str8 = @"1"; NSLog(@"str8 = %d",[str8 boolValue]);
6.截取与分隔
//分割字符串 NSString *str9 = @"aa,bbb,cc,dddd,e,ffff"; NSArray *arr = [str9 componentsSeparatedByString:@","]; //遍历数组 for(NSString *s1 in arr){ NSLog(@"s1 = %@",s1); } NSString *str10 = @"abcdefg"; char s2 = [str10 characterAtIndex:2]; NSLog(@"s2 = %c",s2); //截取字符串 NSString *str11 = @"you are my sunshine"; //截取到index之前的字符 NSString *substr1 = [str11 substringToIndex:3]; NSLog(@"sub1 = %@",substr1); //从index位置开始往后截取 NSString *substr2 = [str11 substringFromIndex:8]; NSLog(@"sub2 = %@",substr2); //利用NSRange来截取某个范围的字符 NSRange range = NSMakeRange(5, 13); NSString *substr3 = [str11 substringWithRange:range]; NSLog(@"sub3 = %@",substr3);
7.拼接字符串
//拼接字符串 NSString *str12 = @"少年易老学难成"; NSString *str13 = @"一寸光阴不可轻"; //方法一 NSString *string1 = [NSString stringWithFormat:@"%@ %@",str12,str13]; NSLog(@"string1 = %@",string1); //方法二 NSString *string2 = [str12 stringByAppendingString:str13]; NSLog(@"string2 = %@",string2); //方法三 NSString *string3 = [str13 stringByAppendingFormat:@" %@",str13]; NSLog(@"string3 = %@",string3);
@未完待续
标签:
原文地址:http://www.cnblogs.com/studyController/p/5630541.html