字符串,NSSet 集合,NSDate日期的类,block的四种形式,数组的遍历相关总结
来源:互联网 发布:支付境外软件服务费 编辑:程序博客网 时间:2024/05/28 03:03
1.用字面量的方式创建一个字符串的对象NSString *str=@"文世倾wenshixuandhjhgfdg";2.打印字符串的长度NSLog(@“%ld”,str.length);——->213.通过下标获取字符串的内容(打印的是汉字的话用”%C”)NSLog(@"%c",[str characterAtIndex:4]);——->e4.用便利构造器的方法创建字符串对象NSString *str=[NSString stringWithString:@“文世轩”];//NSString *str=@“niahao,sadfaerzhty,ujk,m";5.打印指定下标到结尾的字符串内容NSLog(@"%@",[str substringFromIndex:5]);——->o,sadfaerzhty,ujk,m 打印开头到指定下标的字符串内容NSLog(@"%@",[str substringToIndex:5]);———>niaha6.字符串截取(1)NSRange rang={3,7}; NSLog(@“%@“,[str strsubstringWithRange:rang]);nsstring *s=[str strsubstringWithRange:rang]NSLog(@“%@“,s);———>hao,sad(2)NSLog(@"%@",[str substringWithRange:NSMakeRange(3, 7)]);——->hao,sad7.判断两个字符串是否相等NSString *str1=@"文世轩";NSString *str2=@"文世倾";NSLog(@"%d",[str1 isEqualTo:str2]);——->08. NSString *str=@"打了八只蚊子"; NSString *prefix=@"打了"; NSString *prefix1=@"蚊子";(1)前缀是否是”打了” NSLog(@"%d",[str hasPrefix:prefix]);——->1(不是为0)(2)后缀是否是”蚊子” NSLog(@"%d",[str hasSuffix:prefix1]);——->1(不是为0)9.把字符串变成基本数据类型 NSString *str=@"12345"; NSInteger num=[str integerValue]; NSLog(@"%ld",num);——->1234510.把任意类型转换成字符串类型CGFloat g=3.14;NSInteger a=100;NSString *str=[NSString stringWithFormat:@"%ld,%g,%@",a,g,@"ningzhiyuan"]; NSLog(@"%@",str);----11.(1)将所有小写字母变大写NSString *str=@"DFD123laj汉字kjdss";NSLog(@"%@",[str uppercaseString]);——->DFD123LAJ汉字KJDSS(2)将所有大写转换成小写NSString *str1=@"SfdhgDKFJDLF";NSLog(@"%@",[str1 lowercaseString]);——->dfd123laj汉字kjdss(3)首字母大写NSLog(@“%@",str.capitalizedString);———>Dfd123Laj汉字Kjdss12.字符串比较NSString *str=@"nihao";NSString *newstr=@"buhao";NSLog(@"%ld",[str compare:newstr]);——->113.字符串的拼接NSString *str=@"sadjfkasd";NSString *str1=@"askjzkncm63210";NSString *newstr=[str stringByAppendingString:str1];NSLog(@“%@",newstr);———>sadjfkasdaskjzkncm6321014.替换NSString *str=@"12312133";NSString *str1=[str stringByReplacingOccurrencesOfString:@"12"withString:@"*"];NSLog(@“%@",str1);——->*3*133可变字符串1.创建(1)NSMutableString *str=[[NSMutableString alloc] initWithString:@"anyichen"]; NSLog(@“%@",str);——->anyichen(2)NSMutableString *str1=[NSMutableString stringWithString:@"wenshixuan"]; NSLog(@“%@",str1);———>wenshixuan2.插入NSMutableString *str=[NSMutableString stringWithString:@"anyihcen"];[str insertString:@"安逸臣" atIndex:3]; NSLog(@“%@“,str);——->any安逸臣ihcen3.替换NSMutableString *str1=[NSMutableString stringWithString:@"anyichen"];[str1 replaceCharactersInRange:NSMakeRange(3, 3) withString:@“宁致远"];NSLog(@“%@",str1);——->any宁致远en4.字符串拼接(1)NSMutableString *str=[NSMutableString stringWithString:@"xiaoyahuizi"];[str appendString:@"akjf"];NSLog(@“%@“,str);——->xiaoyahuiziakjf(2)NSMutableString *str1=[NSMutableString stringWithString:@"anleyan"];NSString *str2=@"sajfkl";NSString *newstr=[str1 stringByAppendingString:str2];NSLog(@“%@",newstr);——-> anleyansajfklNSSet 集合(1)便利构造器创建NSSet *set=[NSSet setWithObjects:@"1",@"2",@"3",@"4",@"5",@"2", nil];(2)一般创建NSSet *set1=[[NSSet alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];(3)可变集合NSMutableSet *set=[NSMutableSet setWithObjects:@"1",@"2",@"4",@"3",@"5", nil];(4)添加元素[set addObject:@“6"];(5)删除元素[set removeObject:@“1"];(6)清除所有[set removeAllObjects];(7)快速枚举for (NSString *str in set) {NSLog(@“%@",str);}NSDate日期的类NSDate *date=[NSDate date];(1)获取一下当前所在时区,NSTimeZone *zone=[NSTimeZone systemTimeZone];(2)获取一下和0时区相差的秒数NSInteger second=[zone secondsFromGMTForDate:date];(3)通过相差的秒数,能获取到现在的时间NSDate *localdate=[NSDate dateWithTimeIntervalSinceNow:second];(4)明天此时的时间NSDate *tomorrowDate =[NSDate dateWithTimeIntervalSinceNow:(32*3600)];NSLog(@"%@",tomorrowDate );(5)昨天此时的时间NSDate *yesterday=[NSDate dateWithTimeIntervalSinceNow:-(16*3600)];NSLog(@"%@",yesterday);(6)时间间隔对应的double类型计算两个时间对象的时间间隔NSTimeInterval interval=[tomorrowDate timeIntervalSinceDate:date];NSLog(@"%g",interval);(7)NSDate->NSString第一种方式NSDate *date=[NSDate date];NSString *dateStr=[NSString stringWithFormat:@"%@",date];第二种方式,时间的格式yyyy-MM-dd HH-mm-ss y-年,M-月,d-日,H-24小时制,,h-12小时制,,m-分,,s-秒NSDateFormatter *formatter=[[NSDateFormatter alloc] init];[formatter setDateFormat:@"yyyy-MM-dd HH-mm-ss"];NSDate *date=[NSDate date];NSString *strDate=[formatter stringFromDate:date];字符串->NSDate//使用前要先定义格式NSDateFormatter *formatter=[[NSDateFormatter alloc] init];[formatter setDateFormat:@"yyyy-MM-dd HH-mm-ss"];NSString *timeStr=@"2015-7-23 17-18-10";NSDate *date=[formatter dateFromString:timeStr];一.block的四种形式1.无参数,无返回值void(^block)()=^(){NSLog(@“今天正热”);};block();——->今天正热2.有参数,无返回值void(^block)(int, int)=^(int a, int b){int max=a>b?a:b;NSLog(@“%d”,max);};block(10,20);———>20void(^block)(NSString *)=^(NSString *str){NSLog(@“%@”,str);};block(@“nihao”);———->nihao3.有返回值,无参数int (^block)()=^(){return (int)100;};NSLog(@“%d”,block());4.有参数,有返回值NSString *(^block)(NSArray *,NSInteger)=^(NSArray *arr,NSIteger index){return arr[index];};block(@[@“1”,@“2”,@“3”,@“4”],1);———->2数组的遍历1.block 遍历数组NSArray *arr=[NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6", nil];[arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {NSLog(@“%@",obj);}];2.forinNSArray *arr=[NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6", nil]; for (NSString *str in arr) { NSLog(@"%@",str); }3.forNSArray *arr=[NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6", nil];for(NSInteger i=0;i<arr.count;i++){NSLog(@“%@”,arr[i]);}4.迭代器NSArray *arr=@[@"1",@"2",@"3",@"4"];NSEnumerator *enumerator= [arr objectEnumerator]; NSString *str=nil; while (str = [enumerator nextObject]) { NSLog(@"%@",str); }//数组中的字符串拼接成为一个大的字符串NSString *string = [array componentsJoinedByString:@","];
0 0
- 字符串,NSSet 集合,NSDate日期的类,block的四种形式,数组的遍历相关总结
- 迭代器,类型的互相转换,NSDate,集合NSSet
- 遍历集合、数组的四种方法
- NSDate日期的类
- NSSet 集合创建,获取,遍历,可变集合的删除 - iOS
- NSSet 集合创建,获取,遍历,可变集合的删除
- Nsset NSdictionary的相关
- Java遍历集合、数组的四种方法
- Java遍历集合、数组的四种方法
- 集合类的4种遍历方式Block遍历,快速遍历,枚举器,普通遍历
- NSDate 日期类的使用
- iOS 日期(NSDate)的常用操作总结
- Map集合的四种遍历方式总结
- ios基础-字符串和数组的block遍历
- NSArray,NSSet等集合类的使用
- NSSet、NSArray的日期排序
- IOS 中数组、集合、字典的使用(NSArray, NSSet, NSDictionary)
- swift 下 String 转 NSdate 相互转化,一些日期相关的总结
- LeetCode----Find the Duplicate Number
- 周记——20151102
- 写文件fflush问题
- TextView java 代码字体加粗
- Unique Binary Search Trees
- 字符串,NSSet 集合,NSDate日期的类,block的四种形式,数组的遍历相关总结
- C#基础
- 英文版Windows 10中MATLAB中文字符乱码的问题
- Android studio和eclipse为类创建注释
- ORA-38856: Cannot mark instance UNNAMED_INSTANCE_2 (redo thread 2) as enabled
- 【C#】应用程序
- RSA 非对称加密
- CSS3实战:第一天
- 刚开始的感想